
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/modules/codegen.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:24:05 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Code Generation &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
    <script src="../_static/jquery.js"></script>
    <script src="../_static/underscore.js"></script>
    <script src="../_static/doctools.js"></script>
    <script src="../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../live.sympy.org/static/live-sphinx.js"></script>
    
    <link rel="shortcut icon" href="../_static/sympy-notailtext-favicon.ico"/>
    <link href="codegen.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Combinatorics" href="combinatorics/index.html" />
    <link rel="prev" title="Category Theory" href="categories.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="combinatorics/index.html" title="Combinatorics"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="categories.html" title="Category Theory"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">SymPy Modules Reference</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Code Generation</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="code-generation">
<span id="codegen-prose"></span><h1>Code Generation<a class="headerlink" href="#code-generation" title="Permalink to this headline">¶</a></h1>
<p>Several submodules in SymPy allow one to generate directly compilable and
executable code in a variety of different programming languages from SymPy
expressions. In addition, there are functions that generate Python importable
objects that can evaluate SymPy expressions very efficiently.</p>
<p>We will start with a brief introduction to the components that make up the code
generation capabilities of SymPy.</p>
<section id="introduction">
<h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>There are four main levels of abstractions:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">expression</span>
   <span class="o">|</span>
<span class="n">code</span> <span class="n">printers</span>
   <span class="o">|</span>
<span class="n">code</span> <span class="n">generators</span>
   <span class="o">|</span>
<span class="n">autowrap</span>
</pre></div>
</div>
<p><a class="reference internal" href="utilities/autowrap.html#module-sympy.utilities.autowrap" title="sympy.utilities.autowrap"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sympy.utilities.autowrap</span></code></a> uses codegen, and codegen uses the code
printers. <a class="reference internal" href="utilities/autowrap.html#module-sympy.utilities.autowrap" title="sympy.utilities.autowrap"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sympy.utilities.autowrap</span></code></a> does everything: it lets you go
from SymPy expression to numerical function in the same Python process in one
step. Codegen is actual code generation, i.e., to compile and use later, or to
include in some larger project.</p>
<p>The code printers translate the SymPy objects into actual code, like <code class="docutils literal notranslate"><span class="pre">abs(x)</span>
<span class="pre">-&gt;</span> <span class="pre">fabs(x)</span></code> (for C).</p>
<p>The code printers don’t print optimal code in many cases. An example of this is
powers in C. <code class="docutils literal notranslate"><span class="pre">x**2</span></code> prints as <code class="docutils literal notranslate"><span class="pre">pow(x,</span> <span class="pre">2)</span></code> instead of <code class="docutils literal notranslate"><span class="pre">x*x</span></code>.  Other
optimizations (like mathematical simplifications) should happen before the code
printers.</p>
<p>Currently, <a class="reference internal" href="simplify/simplify.html#sympy.simplify.cse_main.cse" title="sympy.simplify.cse_main.cse"><code class="xref py py-func docutils literal notranslate"><span class="pre">sympy.simplify.cse_main.cse()</span></code></a> is not applied automatically anywhere in this
chain. It ideally happens at the codegen level, or somewhere above it.</p>
<p>We will iterate through the levels below.</p>
<p>The following three lines will be used to setup each example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">init_printing</span><span class="p">(</span><span class="n">use_unicode</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">a</span><span class="p">,</span> <span class="n">e</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">Z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">beta</span><span class="p">,</span> <span class="n">omega</span><span class="p">,</span> <span class="n">tau</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">,</span> <span class="n">g</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;f, g&#39;</span><span class="p">,</span> <span class="bp">cls</span><span class="o">=</span><span class="n">Function</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="code-printers-sympy-printing">
<h2>Code printers (sympy.printing)<a class="headerlink" href="#code-printers-sympy-printing" title="Permalink to this headline">¶</a></h2>
<p>This is where the meat of code generation is; the translation of SymPy
actually more like a lightweight version of codegen for Python, and
Python (<a class="reference internal" href="printing.html#sympy.printing.pycode.pycode" title="sympy.printing.pycode.pycode"><code class="xref py py-func docutils literal notranslate"><span class="pre">sympy.printing.pycode.pycode()</span></code></a>), and
<a class="reference internal" href="printing.html#sympy.printing.lambdarepr.lambdarepr" title="sympy.printing.lambdarepr.lambdarepr"><code class="xref py py-func docutils literal notranslate"><span class="pre">sympy.printing.lambdarepr.lambdarepr()</span></code></a>, which supports many libraries
(like NumPy), and Aesara
(<a class="reference internal" href="printing.html#sympy.printing.aesaracode.aesara_function" title="sympy.printing.aesaracode.aesara_function"><code class="xref py py-func docutils literal notranslate"><span class="pre">sympy.printing.aesaracode.aesara_function()</span></code></a>). The code printers are
special cases of the other prints in SymPy (str printer, pretty printer, etc.).</p>
<p>An important distinction is that the code printer has to deal with assignments
(using the <a class="reference internal" href="#sympy.codegen.ast.Assignment" title="sympy.codegen.ast.Assignment"><code class="xref py py-class docutils literal notranslate"><span class="pre">sympy.codegen.ast.Assignment</span></code></a> object). This serves as
building blocks for the code printers and hence the <code class="docutils literal notranslate"><span class="pre">codegen</span></code> module.  An
example that shows the use of <code class="docutils literal notranslate"><span class="pre">Assignment</span></code> in C code:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Assignment</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ccode</span><span class="p">(</span><span class="n">Assignment</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)))</span>
<span class="go">x = y + 1;</span>
</pre></div>
</div>
<p>Here is another simple example of printing a C version of a SymPy expression:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="p">(</span><span class="n">Rational</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">Z</span> <span class="o">*</span> <span class="n">k</span> <span class="o">*</span> <span class="p">(</span><span class="n">e</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span> <span class="o">/</span> <span class="n">r</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">    2</span>
<span class="go">-Z⋅e ⋅k</span>
<span class="go">────────</span>
<span class="go">  2⋅r</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ccode</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
<span class="go">-1.0/2.0*Z*pow(e, 2)*k/r</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">real</span><span class="p">,</span> <span class="n">float80</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ccode</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">assign_to</span><span class="o">=</span><span class="s2">&quot;E&quot;</span><span class="p">,</span> <span class="n">type_aliases</span><span class="o">=</span><span class="p">{</span><span class="n">real</span><span class="p">:</span> <span class="n">float80</span><span class="p">})</span>
<span class="go">E = -1.0L/2.0L*Z*powl(e, 2)*k/r;</span>
</pre></div>
</div>
<p>To generate code with some math functions provided by e.g. the C99 standard we need
to import functions from <a class="reference internal" href="#module-sympy.codegen.cfunctions" title="sympy.codegen.cfunctions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sympy.codegen.cfunctions</span></code></a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.cfunctions</span> <span class="kn">import</span> <span class="n">expm1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ccode</span><span class="p">(</span><span class="n">expm1</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">standard</span><span class="o">=</span><span class="s1">&#39;C99&#39;</span><span class="p">)</span>
<span class="go">expm1(x)</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">Piecewise</span></code> expressions are converted into conditionals. If an <code class="docutils literal notranslate"><span class="pre">assign_to</span></code>
variable is provided an if statement is created, otherwise the ternary operator
is used. Note that if the <code class="docutils literal notranslate"><span class="pre">Piecewise</span></code> lacks a default term, represented by
<code class="docutils literal notranslate"><span class="pre">(expr,</span> <span class="pre">True)</span></code> then an error will be thrown.  This is to prevent generating
an expression that may not evaluate to anything. A use case for <code class="docutils literal notranslate"><span class="pre">Piecewise</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">Piecewise</span><span class="p">((</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">x</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">),</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="kc">True</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fcode</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">tau</span><span class="p">))</span>
<span class="go">      if (x &gt; 0) then</span>
<span class="go">         tau = x + 1</span>
<span class="go">      else</span>
<span class="go">         tau = x</span>
<span class="go">      end if</span>
</pre></div>
</div>
<p>The various printers also tend to support <code class="docutils literal notranslate"><span class="pre">Indexed</span></code> objects well. With
<code class="docutils literal notranslate"><span class="pre">contract=True</span></code> these expressions will be turned into loops, whereas
<code class="docutils literal notranslate"><span class="pre">contract=False</span></code> will just print the assignment expression that should be
looped over:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">len_y</span> <span class="o">=</span> <span class="mi">5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mat_1</span> <span class="o">=</span> <span class="n">IndexedBase</span><span class="p">(</span><span class="s1">&#39;mat_1&#39;</span><span class="p">,</span> <span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="n">len_y</span><span class="p">,))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mat_2</span> <span class="o">=</span> <span class="n">IndexedBase</span><span class="p">(</span><span class="s1">&#39;mat_2&#39;</span><span class="p">,</span> <span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="n">len_y</span><span class="p">,))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Dy</span> <span class="o">=</span> <span class="n">IndexedBase</span><span class="p">(</span><span class="s1">&#39;Dy&#39;</span><span class="p">,</span> <span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="n">len_y</span><span class="o">-</span><span class="mi">1</span><span class="p">,))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">i</span> <span class="o">=</span> <span class="n">Idx</span><span class="p">(</span><span class="s1">&#39;i&#39;</span><span class="p">,</span> <span class="n">len_y</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">eq</span> <span class="o">=</span> <span class="n">Eq</span><span class="p">(</span><span class="n">Dy</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="p">(</span><span class="n">mat_1</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">mat_1</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">/</span> <span class="p">(</span><span class="n">mat_2</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">mat_2</span><span class="p">[</span><span class="n">i</span><span class="p">]))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">jscode</span><span class="p">(</span><span class="n">eq</span><span class="o">.</span><span class="n">rhs</span><span class="p">,</span> <span class="n">assign_to</span><span class="o">=</span><span class="n">eq</span><span class="o">.</span><span class="n">lhs</span><span class="p">,</span> <span class="n">contract</span><span class="o">=</span><span class="kc">False</span><span class="p">))</span>
<span class="go">Dy[i] = (mat_1[i + 1] - mat_1[i])/(mat_2[i + 1] - mat_2[i]);</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Res</span> <span class="o">=</span> <span class="n">IndexedBase</span><span class="p">(</span><span class="s1">&#39;Res&#39;</span><span class="p">,</span> <span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="n">len_y</span><span class="p">,))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">j</span> <span class="o">=</span> <span class="n">Idx</span><span class="p">(</span><span class="s1">&#39;j&#39;</span><span class="p">,</span> <span class="n">len_y</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">eq</span> <span class="o">=</span> <span class="n">Eq</span><span class="p">(</span><span class="n">Res</span><span class="p">[</span><span class="n">j</span><span class="p">],</span> <span class="n">mat_1</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">*</span><span class="n">mat_2</span><span class="p">[</span><span class="n">j</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">jscode</span><span class="p">(</span><span class="n">eq</span><span class="o">.</span><span class="n">rhs</span><span class="p">,</span> <span class="n">assign_to</span><span class="o">=</span><span class="n">eq</span><span class="o">.</span><span class="n">lhs</span><span class="p">,</span> <span class="n">contract</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="go">for (var j=0; j&lt;5; j++){</span>
<span class="go">   Res[j] = 0;</span>
<span class="go">}</span>
<span class="go">for (var j=0; j&lt;5; j++){</span>
<span class="go">   for (var j=0; j&lt;5; j++){</span>
<span class="go">      Res[j] = Res[j] + mat_1[j]*mat_2[j];</span>
<span class="go">   }</span>
<span class="go">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">jscode</span><span class="p">(</span><span class="n">eq</span><span class="o">.</span><span class="n">rhs</span><span class="p">,</span> <span class="n">assign_to</span><span class="o">=</span><span class="n">eq</span><span class="o">.</span><span class="n">lhs</span><span class="p">,</span> <span class="n">contract</span><span class="o">=</span><span class="kc">False</span><span class="p">))</span>
<span class="go">Res[j] = mat_1[j]*mat_2[j];</span>
</pre></div>
</div>
<p>Custom printing can be defined for certain types by passing a dictionary of
“type” : “function” to the <code class="docutils literal notranslate"><span class="pre">user_functions</span></code> kwarg. Alternatively, the
dictionary value can be a list of tuples i.e., <code class="docutils literal notranslate"><span class="pre">[(argument_test,</span>
<span class="pre">cfunction_string)]</span></code>. This can be used to call a custom Octave function:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">custom_functions</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>  <span class="s2">&quot;f&quot;</span><span class="p">:</span> <span class="s2">&quot;existing_octave_fcn&quot;</span><span class="p">,</span>
<span class="gp">... </span>  <span class="s2">&quot;g&quot;</span><span class="p">:</span> <span class="p">[(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">.</span><span class="n">is_Matrix</span><span class="p">,</span> <span class="s2">&quot;my_mat_fcn&quot;</span><span class="p">),</span>
<span class="gp">... </span>        <span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="ow">not</span> <span class="n">x</span><span class="o">.</span><span class="n">is_Matrix</span><span class="p">,</span> <span class="s2">&quot;my_fcn&quot;</span><span class="p">)]</span>
<span class="gp">... </span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mat</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="n">x</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">octave_code</span><span class="p">(</span><span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">g</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">g</span><span class="p">(</span><span class="n">mat</span><span class="p">),</span> <span class="n">user_functions</span><span class="o">=</span><span class="n">custom_functions</span><span class="p">)</span>
<span class="go">existing_octave_fcn(x) + my_fcn(x) + my_mat_fcn([1 x])</span>
</pre></div>
</div>
<p>An example of Mathematica code printer:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x_</span> <span class="o">=</span> <span class="n">Function</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">x_</span><span class="p">(</span><span class="n">n</span><span class="o">*</span><span class="n">T</span><span class="p">)</span> <span class="o">*</span> <span class="n">sin</span><span class="p">((</span><span class="n">t</span> <span class="o">-</span> <span class="n">n</span><span class="o">*</span><span class="n">T</span><span class="p">)</span> <span class="o">/</span> <span class="n">T</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">expr</span> <span class="o">/</span> <span class="p">((</span><span class="o">-</span><span class="n">T</span><span class="o">*</span><span class="n">n</span> <span class="o">+</span> <span class="n">t</span><span class="p">)</span> <span class="o">/</span> <span class="n">T</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">            ⎛-T⋅n + t⎞</span>
<span class="go">T⋅x(T⋅n)⋅sin⎜────────⎟</span>
<span class="go">            ⎝   T    ⎠</span>
<span class="go">──────────────────────</span>
<span class="go">       -T⋅n + t</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">summation</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mathematica_code</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
<span class="go">T*(x[-T]*Sin[(T + t)/T]/(T + t) + x[T]*Sin[(-T + t)/T]/(-T + t) + x[0]*Sin[t/T</span>
<span class="go">]/t)</span>
</pre></div>
</div>
<p>We can go through a common expression in different languages we support and see
how it works:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">k</span><span class="p">,</span> <span class="n">g1</span><span class="p">,</span> <span class="n">g2</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">I</span><span class="p">,</span> <span class="n">S</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s2">&quot;k, gamma_1, gamma_2, r, I, S&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">k</span> <span class="o">*</span> <span class="n">g1</span> <span class="o">*</span> <span class="n">g2</span> <span class="o">/</span> <span class="p">(</span><span class="n">r</span><span class="o">**</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">expr</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">I</span> <span class="o">*</span> <span class="n">S</span> <span class="o">*</span> <span class="p">(</span><span class="mi">3</span> <span class="o">*</span> <span class="p">(</span><span class="n">cos</span><span class="p">(</span><span class="n">beta</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">            ⎛     2       ⎞</span>
<span class="go">I⋅S⋅γ₁⋅γ₂⋅k⋅⎝3⋅cos (β) - 1⎠</span>
<span class="go">───────────────────────────</span>
<span class="go">              3</span>
<span class="go">             r</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">jscode</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">assign_to</span><span class="o">=</span><span class="s2">&quot;H_is&quot;</span><span class="p">))</span>
<span class="go">H_is = I*S*gamma_1*gamma_2*k*(3*Math.pow(Math.cos(beta), 2) - 1)/Math.pow(r, 3);</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ccode</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">assign_to</span><span class="o">=</span><span class="s2">&quot;H_is&quot;</span><span class="p">,</span> <span class="n">standard</span><span class="o">=</span><span class="s1">&#39;C89&#39;</span><span class="p">))</span>
<span class="go">H_is = I*S*gamma_1*gamma_2*k*(3*pow(cos(beta), 2) - 1)/pow(r, 3);</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fcode</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">assign_to</span><span class="o">=</span><span class="s2">&quot;H_is&quot;</span><span class="p">))</span>
<span class="go">      H_is = I*S*gamma_1*gamma_2*k*(3*cos(beta)**2 - 1)/r**3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">julia_code</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">assign_to</span><span class="o">=</span><span class="s2">&quot;H_is&quot;</span><span class="p">))</span>
<span class="go">H_is = I.*S.*gamma_1.*gamma_2.*k.*(3*cos(beta).^2 - 1)./r.^3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">octave_code</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">assign_to</span><span class="o">=</span><span class="s2">&quot;H_is&quot;</span><span class="p">))</span>
<span class="go">H_is = I.*S.*gamma_1.*gamma_2.*k.*(3*cos(beta).^2 - 1)./r.^3;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">rust_code</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">assign_to</span><span class="o">=</span><span class="s2">&quot;H_is&quot;</span><span class="p">))</span>
<span class="go">H_is = I*S*gamma_1*gamma_2*k*(3*beta.cos().powi(2) - 1)/r.powi(3);</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">mathematica_code</span><span class="p">(</span><span class="n">expr</span><span class="p">))</span>
<span class="go">I*S*gamma_1*gamma_2*k*(3*Cos[beta]^2 - 1)/r^3</span>
</pre></div>
</div>
</section>
<section id="codegen-sympy-utilities-codegen">
<h2>Codegen (sympy.utilities.codegen)<a class="headerlink" href="#codegen-sympy-utilities-codegen" title="Permalink to this headline">¶</a></h2>
<p>This module deals with creating compilable code from SymPy expressions. This is
lower level than autowrap, as it doesn’t actually attempt to compile the code,
but higher level than the printers, as it generates compilable files (including
header files), rather than just code snippets.</p>
<p>The user friendly functions, here, are <code class="docutils literal notranslate"><span class="pre">codegen</span></code> and <code class="docutils literal notranslate"><span class="pre">make_routine</span></code>.
<code class="docutils literal notranslate"><span class="pre">codegen</span></code> takes a list of <code class="docutils literal notranslate"><span class="pre">(variable,</span> <span class="pre">expression)</span></code> pairs and a language (C,
F95, and Octave/Matlab are supported). It returns, as strings, a code file and
a header file (for relevant languages). The variables are created as functions
that return the value of the expression as output.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The <code class="docutils literal notranslate"><span class="pre">codegen</span></code> callable is not in the sympy namespace automatically,
to use it you must first import <code class="docutils literal notranslate"><span class="pre">codegen</span></code> from <code class="docutils literal notranslate"><span class="pre">sympy.utilities.codegen</span></code></p>
</div>
<p>For instance:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.codegen</span> <span class="kn">import</span> <span class="n">codegen</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">length</span><span class="p">,</span> <span class="n">breadth</span><span class="p">,</span> <span class="n">height</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;length, breadth, height&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[(</span><span class="n">c_name</span><span class="p">,</span> <span class="n">c_code</span><span class="p">),</span> <span class="p">(</span><span class="n">h_name</span><span class="p">,</span> <span class="n">c_header</span><span class="p">)]</span> <span class="o">=</span> \
<span class="gp">... </span><span class="n">codegen</span><span class="p">((</span><span class="s1">&#39;volume&#39;</span><span class="p">,</span> <span class="n">length</span><span class="o">*</span><span class="n">breadth</span><span class="o">*</span><span class="n">height</span><span class="p">),</span> <span class="s2">&quot;C99&quot;</span><span class="p">,</span> <span class="s2">&quot;test&quot;</span><span class="p">,</span>
<span class="gp">... </span>        <span class="n">header</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">empty</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">c_name</span><span class="p">)</span>
<span class="go">test.c</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">c_code</span><span class="p">)</span>
<span class="go">#include &quot;test.h&quot;</span>
<span class="go">#include &lt;math.h&gt;</span>
<span class="go">double volume(double breadth, double height, double length) {</span>
<span class="go">   double volume_result;</span>
<span class="go">   volume_result = breadth*height*length;</span>
<span class="go">   return volume_result;</span>
<span class="go">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">h_name</span><span class="p">)</span>
<span class="go">test.h</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">c_header</span><span class="p">)</span>
<span class="go">#ifndef PROJECT__TEST__H</span>
<span class="go">#define PROJECT__TEST__H</span>
<span class="go">double volume(double breadth, double height, double length);</span>
<span class="go">#endif</span>
</pre></div>
</div>
<p>Various flags to <code class="docutils literal notranslate"><span class="pre">codegen</span></code> let you modify things. The project name for
preprocessor instructions can be varied using <code class="docutils literal notranslate"><span class="pre">project</span></code>. Variables listed as
global variables in arg <code class="docutils literal notranslate"><span class="pre">global_vars</span></code> will not show up as function arguments.</p>
<p><code class="docutils literal notranslate"><span class="pre">language</span></code> is a case-insensitive string that indicates the source code
language. Currently, <code class="docutils literal notranslate"><span class="pre">C</span></code>, <code class="docutils literal notranslate"><span class="pre">F95</span></code> and <code class="docutils literal notranslate"><span class="pre">Octave</span></code> are supported. <code class="docutils literal notranslate"><span class="pre">Octave</span></code>
generates code compatible with both Octave and Matlab.</p>
<p><code class="docutils literal notranslate"><span class="pre">header</span></code> when True, a header is written on top of each source file. <code class="docutils literal notranslate"><span class="pre">empty</span></code>
when True, empty lines are used to structure the code. With
<code class="docutils literal notranslate"><span class="pre">argument_sequence</span></code> a sequence of arguments for the routine can be defined in
a preferred order.</p>
<p><code class="docutils literal notranslate"><span class="pre">prefix</span></code> defines a prefix for the names of the files that contain the source
code.  If omitted, the name of the first name_expr tuple is used.</p>
<p><code class="docutils literal notranslate"><span class="pre">to_files</span></code> when True, the code will be written to one or more files with the
given prefix.</p>
<p>Here is an example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">[(</span><span class="n">f_name</span><span class="p">,</span> <span class="n">f_code</span><span class="p">),</span> <span class="n">header</span><span class="p">]</span> <span class="o">=</span> <span class="n">codegen</span><span class="p">((</span><span class="s2">&quot;volume&quot;</span><span class="p">,</span> <span class="n">length</span><span class="o">*</span><span class="n">breadth</span><span class="o">*</span><span class="n">height</span><span class="p">),</span>
<span class="gp">... </span>    <span class="s2">&quot;F95&quot;</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">empty</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">argument_sequence</span><span class="o">=</span><span class="p">(</span><span class="n">breadth</span><span class="p">,</span> <span class="n">length</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">global_vars</span><span class="o">=</span><span class="p">(</span><span class="n">height</span><span class="p">,))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">f_code</span><span class="p">)</span>
<span class="go">REAL*8 function volume(breadth, length)</span>
<span class="go">implicit none</span>
<span class="go">REAL*8, intent(in) :: breadth</span>
<span class="go">REAL*8, intent(in) :: length</span>
<span class="go">volume = breadth*height*length</span>
<span class="go">end function</span>
</pre></div>
</div>
<p>The method <code class="docutils literal notranslate"><span class="pre">make_routine</span></code> creates a <code class="docutils literal notranslate"><span class="pre">Routine</span></code> object, which represents an
evaluation routine for a set of expressions. This is only good for internal use
by the CodeGen objects, as an intermediate representation from SymPy expression
to generated code.  It is not recommended to make a <code class="docutils literal notranslate"><span class="pre">Routine</span></code> object
yourself. You should instead use <code class="docutils literal notranslate"><span class="pre">make_routine</span></code> method. <code class="docutils literal notranslate"><span class="pre">make_routine</span></code> in
turn calls the <code class="docutils literal notranslate"><span class="pre">routine</span></code> method of the CodeGen object depending upon the
language of choice. This creates the internal objects representing assignments
and so on, and creates the <code class="docutils literal notranslate"><span class="pre">Routine</span></code> class with them.</p>
<p>The various codegen objects such as <code class="docutils literal notranslate"><span class="pre">Routine</span></code> and <code class="docutils literal notranslate"><span class="pre">Variable</span></code> aren’t SymPy
objects (they don’t subclass from Basic).</p>
<p>For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.codegen</span> <span class="kn">import</span> <span class="n">make_routine</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.hydrogen</span> <span class="kn">import</span> <span class="n">R_nl</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">R_nl</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">routine</span> <span class="o">=</span> <span class="n">make_routine</span><span class="p">(</span><span class="s1">&#39;my_routine&#39;</span><span class="p">,</span> <span class="n">expr</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">arg</span><span class="o">.</span><span class="n">result_var</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">routine</span><span class="o">.</span><span class="n">results</span><span class="p">]</span>   
<span class="go">[result₅₁₄₂₃₄₁₆₈₁₃₉₇₇₁₉₄₂₈]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">arg</span><span class="o">.</span><span class="n">expr</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">routine</span><span class="o">.</span><span class="n">results</span><span class="p">]</span>
<span class="go">⎡                __________                                          ⎤</span>
<span class="go">⎢          y    ╱ (2 - y)!   -2⋅x                                    ⎥</span>
<span class="go">⎢4⋅√6⋅(4⋅x) ⋅  ╱  ──────── ⋅ℯ    ⋅assoc_laguerre(2 - y, 2⋅y + 1, 4⋅x)⎥</span>
<span class="go">⎢            ╲╱   (y + 3)!                                           ⎥</span>
<span class="go">⎢────────────────────────────────────────────────────────────────────⎥</span>
<span class="go">⎣                                 3                                  ⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">arg</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">routine</span><span class="o">.</span><span class="n">arguments</span><span class="p">]</span>
<span class="go">[x, y]</span>
</pre></div>
</div>
<p>Another more complicated example with a mixture of specified and
automatically-assigned names.  Also has Matrix output:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">routine</span> <span class="o">=</span> <span class="n">make_routine</span><span class="p">(</span><span class="s1">&#39;fcn&#39;</span><span class="p">,</span> <span class="p">[</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">,</span> <span class="n">Eq</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">Eq</span><span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="n">x</span> <span class="o">+</span> <span class="n">r</span><span class="p">),</span> <span class="n">Matrix</span><span class="p">([[</span><span class="n">x</span><span class="p">,</span> <span class="mi">2</span><span class="p">]])])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">arg</span><span class="o">.</span><span class="n">result_var</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">routine</span><span class="o">.</span><span class="n">results</span><span class="p">]</span>   
<span class="go">[result_5397460570204848505]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">arg</span><span class="o">.</span><span class="n">expr</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">routine</span><span class="o">.</span><span class="n">results</span><span class="p">]</span>
<span class="go">[x⋅y]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">arg</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">routine</span><span class="o">.</span><span class="n">arguments</span><span class="p">]</span>   
<span class="go">[x, y, a, r, out_8598435338387848786]</span>
</pre></div>
</div>
<p>We can examine the various arguments more closely:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.codegen</span> <span class="kn">import</span> <span class="p">(</span><span class="n">InputArgument</span><span class="p">,</span> <span class="n">OutputArgument</span><span class="p">,</span>
<span class="gp">... </span>                                     <span class="n">InOutArgument</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">a</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">routine</span><span class="o">.</span><span class="n">arguments</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">InputArgument</span><span class="p">)]</span>
<span class="go">[x, y]</span>

<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">a</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">routine</span><span class="o">.</span><span class="n">arguments</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">OutputArgument</span><span class="p">)]</span>  
<span class="go">[a, out_8598435338387848786]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">a</span><span class="o">.</span><span class="n">expr</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">routine</span><span class="o">.</span><span class="n">arguments</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">OutputArgument</span><span class="p">)]</span>
<span class="go">[1, [x  2]]</span>

<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">a</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">routine</span><span class="o">.</span><span class="n">arguments</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">InOutArgument</span><span class="p">)]</span>
<span class="go">[r]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">a</span><span class="o">.</span><span class="n">expr</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">routine</span><span class="o">.</span><span class="n">arguments</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">InOutArgument</span><span class="p">)]</span>
<span class="go">[r + x]</span>
</pre></div>
</div>
<p>The full API reference can be viewed <a class="reference internal" href="utilities/codegen.html#codegen-api"><span class="std std-ref">here</span></a>.</p>
</section>
<section id="autowrap">
<h2>Autowrap<a class="headerlink" href="#autowrap" title="Permalink to this headline">¶</a></h2>
<p>Autowrap automatically generates code, writes it to disk, compiles it, and
imports it into the current session. Main functions of this module are
<code class="docutils literal notranslate"><span class="pre">autowrap</span></code>, <code class="docutils literal notranslate"><span class="pre">binary_function</span></code>, and <code class="docutils literal notranslate"><span class="pre">ufuncify</span></code>.</p>
<p>It also automatically converts expressions containing <code class="docutils literal notranslate"><span class="pre">Indexed</span></code> objects into
summations. The classes IndexedBase, Indexed and Idx represent a matrix element
M[i, j]. See <a class="reference internal" href="tensor/index.html#tensor-module"><span class="std std-ref">Tensor</span></a> for more on this.</p>
<p id="id1"><code class="docutils literal notranslate"><span class="pre">autowrap</span></code> creates a wrapper using f2py or Cython and creates a numerical function.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The <code class="docutils literal notranslate"><span class="pre">autowrap</span></code> callable is not in the sympy namespace automatically,
to use it you must first import <code class="docutils literal notranslate"><span class="pre">autowrap</span></code> from <code class="docutils literal notranslate"><span class="pre">sympy.utilities.autowrap</span></code></p>
</div>
<p>The callable returned from autowrap() is a binary Python function, not a SymPy
object. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.autowrap</span> <span class="kn">import</span> <span class="n">autowrap</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="p">((</span><span class="n">x</span> <span class="o">-</span> <span class="n">y</span> <span class="o">+</span> <span class="n">z</span><span class="p">)</span><span class="o">**</span><span class="p">(</span><span class="mi">13</span><span class="p">))</span><span class="o">.</span><span class="n">expand</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">binary_func</span> <span class="o">=</span> <span class="n">autowrap</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>    
<span class="gp">&gt;&gt;&gt; </span><span class="n">binary_func</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>    
<span class="go">-1.0</span>
</pre></div>
</div>
<p>The various flags available with autowrap() help to modify the services
provided by the method. The argument <code class="docutils literal notranslate"><span class="pre">tempdir</span></code> tells autowrap to compile the
code in a specific directory, and leave the files intact when finished. For
instance:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.autowrap</span> <span class="kn">import</span> <span class="n">autowrap</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.qho_1d</span> <span class="kn">import</span> <span class="n">psi_n</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x_</span> <span class="o">=</span> <span class="n">IndexedBase</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y_</span> <span class="o">=</span> <span class="n">IndexedBase</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;m&#39;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">i</span> <span class="o">=</span> <span class="n">Idx</span><span class="p">(</span><span class="s1">&#39;i&#39;</span><span class="p">,</span> <span class="n">m</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">qho</span> <span class="o">=</span> <span class="n">autowrap</span><span class="p">(</span><span class="n">Eq</span><span class="p">(</span><span class="n">y_</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">psi_n</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">x_</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">m</span><span class="p">,</span> <span class="n">omega</span><span class="p">)),</span> <span class="n">tempdir</span><span class="o">=</span><span class="s1">&#39;/tmp&#39;</span><span class="p">)</span>  
</pre></div>
</div>
<p>Checking the Fortran source code in the directory specified reveals this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">subroutine</span> <span class="n">autofunc</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">omega</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">implicit</span> <span class="n">none</span>
<span class="n">INTEGER</span><span class="o">*</span><span class="mi">4</span><span class="p">,</span> <span class="n">intent</span><span class="p">(</span><span class="ow">in</span><span class="p">)</span> <span class="p">::</span> <span class="n">m</span>
<span class="n">REAL</span><span class="o">*</span><span class="mi">8</span><span class="p">,</span> <span class="n">intent</span><span class="p">(</span><span class="ow">in</span><span class="p">)</span> <span class="p">::</span> <span class="n">omega</span>
<span class="n">REAL</span><span class="o">*</span><span class="mi">8</span><span class="p">,</span> <span class="n">intent</span><span class="p">(</span><span class="ow">in</span><span class="p">),</span> <span class="n">dimension</span><span class="p">(</span><span class="mi">1</span><span class="p">:</span><span class="n">m</span><span class="p">)</span> <span class="p">::</span> <span class="n">x</span>
<span class="n">REAL</span><span class="o">*</span><span class="mi">8</span><span class="p">,</span> <span class="n">intent</span><span class="p">(</span><span class="n">out</span><span class="p">),</span> <span class="n">dimension</span><span class="p">(</span><span class="mi">1</span><span class="p">:</span><span class="n">m</span><span class="p">)</span> <span class="p">::</span> <span class="n">y</span>
<span class="n">INTEGER</span><span class="o">*</span><span class="mi">4</span> <span class="p">::</span> <span class="n">i</span>

<span class="n">REAL</span><span class="o">*</span><span class="mi">8</span><span class="p">,</span> <span class="n">parameter</span> <span class="p">::</span> <span class="n">hbar</span> <span class="o">=</span> <span class="mf">1.05457162</span><span class="n">d</span><span class="o">-</span><span class="mi">34</span>
<span class="n">REAL</span><span class="o">*</span><span class="mi">8</span><span class="p">,</span> <span class="n">parameter</span> <span class="p">::</span> <span class="n">pi</span> <span class="o">=</span> <span class="mf">3.14159265358979</span><span class="n">d0</span>
<span class="n">do</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">m</span>
   <span class="n">y</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="n">m</span><span class="o">*</span><span class="n">omega</span><span class="p">)</span><span class="o">**</span><span class="p">(</span><span class="mf">1.0</span><span class="n">d0</span><span class="o">/</span><span class="mf">4.0</span><span class="n">d0</span><span class="p">)</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">4.74126166983329</span><span class="n">d</span><span class="o">+</span><span class="mi">33</span><span class="o">*</span><span class="n">m</span><span class="o">*</span><span class="n">omega</span><span class="o">*</span><span class="n">x</span><span class="p">(</span><span class="n">i</span> <span class="o">&amp;</span>
         <span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="n">hbar</span><span class="o">**</span><span class="p">(</span><span class="mf">1.0</span><span class="n">d0</span><span class="o">/</span><span class="mf">4.0</span><span class="n">d0</span><span class="p">)</span><span class="o">*</span><span class="n">pi</span><span class="o">**</span><span class="p">(</span><span class="mf">1.0</span><span class="n">d0</span><span class="o">/</span><span class="mf">4.0</span><span class="n">d0</span><span class="p">))</span>
<span class="n">end</span> <span class="n">do</span>

<span class="n">end</span> <span class="n">subroutine</span>
</pre></div>
</div>
<p>Using the argument <code class="docutils literal notranslate"><span class="pre">args</span></code> along with it changes argument sequence:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">eq</span> <span class="o">=</span> <span class="n">Eq</span><span class="p">(</span><span class="n">y_</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">psi_n</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">x_</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">m</span><span class="p">,</span> <span class="n">omega</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">qho</span> <span class="o">=</span> <span class="n">autowrap</span><span class="p">(</span><span class="n">eq</span><span class="p">,</span> <span class="n">tempdir</span><span class="o">=</span><span class="s1">&#39;/tmp&#39;</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">[</span><span class="n">y</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">omega</span><span class="p">])</span>  
</pre></div>
</div>
<p>yields:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">subroutine</span> <span class="n">autofunc</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">omega</span><span class="p">)</span>
<span class="n">implicit</span> <span class="n">none</span>
<span class="n">INTEGER</span><span class="o">*</span><span class="mi">4</span><span class="p">,</span> <span class="n">intent</span><span class="p">(</span><span class="ow">in</span><span class="p">)</span> <span class="p">::</span> <span class="n">m</span>
<span class="n">REAL</span><span class="o">*</span><span class="mi">8</span><span class="p">,</span> <span class="n">intent</span><span class="p">(</span><span class="ow">in</span><span class="p">)</span> <span class="p">::</span> <span class="n">omega</span>
<span class="n">REAL</span><span class="o">*</span><span class="mi">8</span><span class="p">,</span> <span class="n">intent</span><span class="p">(</span><span class="n">out</span><span class="p">),</span> <span class="n">dimension</span><span class="p">(</span><span class="mi">1</span><span class="p">:</span><span class="n">m</span><span class="p">)</span> <span class="p">::</span> <span class="n">y</span>
<span class="n">REAL</span><span class="o">*</span><span class="mi">8</span><span class="p">,</span> <span class="n">intent</span><span class="p">(</span><span class="ow">in</span><span class="p">),</span> <span class="n">dimension</span><span class="p">(</span><span class="mi">1</span><span class="p">:</span><span class="n">m</span><span class="p">)</span> <span class="p">::</span> <span class="n">x</span>
<span class="n">INTEGER</span><span class="o">*</span><span class="mi">4</span> <span class="p">::</span> <span class="n">i</span>

<span class="n">REAL</span><span class="o">*</span><span class="mi">8</span><span class="p">,</span> <span class="n">parameter</span> <span class="p">::</span> <span class="n">hbar</span> <span class="o">=</span> <span class="mf">1.05457162</span><span class="n">d</span><span class="o">-</span><span class="mi">34</span>
<span class="n">REAL</span><span class="o">*</span><span class="mi">8</span><span class="p">,</span> <span class="n">parameter</span> <span class="p">::</span> <span class="n">pi</span> <span class="o">=</span> <span class="mf">3.14159265358979</span><span class="n">d0</span>
<span class="n">do</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">m</span>
   <span class="n">y</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="n">m</span><span class="o">*</span><span class="n">omega</span><span class="p">)</span><span class="o">**</span><span class="p">(</span><span class="mf">1.0</span><span class="n">d0</span><span class="o">/</span><span class="mf">4.0</span><span class="n">d0</span><span class="p">)</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">4.74126166983329</span><span class="n">d</span><span class="o">+</span><span class="mi">33</span><span class="o">*</span><span class="n">m</span><span class="o">*</span><span class="n">omega</span><span class="o">*</span><span class="n">x</span><span class="p">(</span><span class="n">i</span> <span class="o">&amp;</span>
         <span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="n">hbar</span><span class="o">**</span><span class="p">(</span><span class="mf">1.0</span><span class="n">d0</span><span class="o">/</span><span class="mf">4.0</span><span class="n">d0</span><span class="p">)</span><span class="o">*</span><span class="n">pi</span><span class="o">**</span><span class="p">(</span><span class="mf">1.0</span><span class="n">d0</span><span class="o">/</span><span class="mf">4.0</span><span class="n">d0</span><span class="p">))</span>
<span class="n">end</span> <span class="n">do</span>

<span class="n">end</span> <span class="n">subroutine</span>
</pre></div>
</div>
<p>The argument <code class="docutils literal notranslate"><span class="pre">verbose</span></code> is boolean, optional and if True, autowrap will not
mute the command line backends. This can be helpful for debugging.</p>
<p>The argument <code class="docutils literal notranslate"><span class="pre">language</span></code> and <code class="docutils literal notranslate"><span class="pre">backend</span></code> are used to change defaults:
<code class="docutils literal notranslate"><span class="pre">Fortran</span></code> and <code class="docutils literal notranslate"><span class="pre">f2py</span></code> to <code class="docutils literal notranslate"><span class="pre">C</span></code> and <code class="docutils literal notranslate"><span class="pre">Cython</span></code>. The argument helpers is used
to define auxiliary expressions needed for the main expression. If the main
expression needs to call a specialized function it should be put in the
<code class="docutils literal notranslate"><span class="pre">helpers</span></code> iterable. Autowrap will then make sure that the compiled main
expression can link to the helper routine. Items should be tuples with
<code class="docutils literal notranslate"><span class="pre">(&lt;function_name&gt;,</span> <span class="pre">&lt;sympy_expression&gt;,</span> <span class="pre">&lt;arguments&gt;)</span></code>. It is mandatory to
supply an argument sequence to helper routines.</p>
<p id="binary-function">Another method available at the <code class="docutils literal notranslate"><span class="pre">autowrap</span></code> level is <code class="docutils literal notranslate"><span class="pre">binary_function</span></code>. It
returns a sympy function. The advantage is that we can have very fast functions
as compared to SymPy speeds. This is because we will be using compiled
functions with Sympy attributes and methods. An illustration:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.autowrap</span> <span class="kn">import</span> <span class="n">binary_function</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.hydrogen</span> <span class="kn">import</span> <span class="n">R_nl</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">psi_nl</span> <span class="o">=</span> <span class="n">R_nl</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">r</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">binary_function</span><span class="p">(</span><span class="s1">&#39;f&#39;</span><span class="p">,</span> <span class="n">psi_nl</span><span class="p">)</span>    
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">r</span><span class="p">)</span><span class="o">.</span><span class="n">evalf</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">subs</span><span class="o">=</span><span class="p">{</span><span class="n">a</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="n">r</span><span class="p">:</span> <span class="mi">2</span><span class="p">})</span>  
<span class="go">0.766</span>
</pre></div>
</div>
<p id="ufuncify-method">While NumPy operations are very efficient for vectorized data but they
sometimes incur unnecessary costs when chained together.
Consider the following operation</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">get_numpy_array</span><span class="p">(</span><span class="o">...</span><span class="p">)</span> 
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">/</span> <span class="n">x</span>
</pre></div>
</div>
<p>The operators <code class="docutils literal notranslate"><span class="pre">sin</span></code> and <code class="docutils literal notranslate"><span class="pre">/</span></code> call routines that execute tight for loops in
<code class="docutils literal notranslate"><span class="pre">C</span></code>. The resulting computation looks something like this</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">for</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">temp</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">for</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">temp</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">];</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>
</div>
<p>This is slightly sub-optimal because</p>
<ol class="arabic simple">
<li><p>We allocate an extra <code class="docutils literal notranslate"><span class="pre">temp</span></code> array</p></li>
<li><p>We walk over <code class="docutils literal notranslate"><span class="pre">x</span></code> memory twice when once would have been sufficient</p></li>
</ol>
<p>A better solution would fuse both element-wise operations into a single for loop</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">for</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">])</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">];</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>
</div>
<p>Statically compiled projects like NumPy are unable to take advantage of such
optimizations. Fortunately, SymPy is able to generate efficient low-level C
or Fortran code. It can then depend on projects like <code class="docutils literal notranslate"><span class="pre">Cython</span></code> or <code class="docutils literal notranslate"><span class="pre">f2py</span></code> to
compile and reconnect that code back up to Python. Fortunately this process is
well automated and a SymPy user wishing to make use of this code generation
should call the <code class="docutils literal notranslate"><span class="pre">ufuncify</span></code> function.</p>
<p><code class="docutils literal notranslate"><span class="pre">ufuncify</span></code> is the third method available with Autowrap module. It basically
implies ‘Universal functions’ and follows an ideology set by NumPy. The main
point of ufuncify as compared to autowrap is that it allows arrays as arguments
and can operate in an element-by-element fashion. The core operation done
element-wise is in accordance to Numpy’s array broadcasting rules. See <a class="reference external" href="https://docs.scipy.org/doc/numpy/reference/ufuncs.html">this</a> for more.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="n">x</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.autowrap</span> <span class="kn">import</span> <span class="n">ufuncify</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ufuncify</span><span class="p">([</span><span class="n">x</span><span class="p">],</span> <span class="n">expr</span><span class="p">)</span> 
</pre></div>
</div>
<p>This function <code class="docutils literal notranslate"><span class="pre">f</span></code> consumes and returns a NumPy array. Generally <code class="docutils literal notranslate"><span class="pre">ufuncify</span></code>
performs at least as well as <code class="docutils literal notranslate"><span class="pre">lambdify</span></code>. If the expression is complicated
then <code class="docutils literal notranslate"><span class="pre">ufuncify</span></code> often significantly outperforms the NumPy backed solution.
Jensen has a good <a class="reference external" href="https://ojensen.wordpress.com/2010/08/10/fast-ufunc-ish-hydrogen-solutions/">blog post</a> on this topic.</p>
<p>Let us see an example for some quantitative analysis:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.hydrogen</span> <span class="kn">import</span> <span class="n">R_nl</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">R_nl</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">               -2⋅x</span>
<span class="go">8⋅x⋅(4 - 4⋅x)⋅ℯ</span>
<span class="go">───────────────────</span>
<span class="go">         3</span>
</pre></div>
</div>
<p>The lambdify function translates SymPy expressions into Python functions,
leveraging a variety of numerical libraries. By default lambdify relies on
implementations in the <code class="docutils literal notranslate"><span class="pre">math</span></code> standard library. Naturally, Raw Python is
faster than SymPy. However it also supports <code class="docutils literal notranslate"><span class="pre">mpmath</span></code> and most notably,
<code class="docutils literal notranslate"><span class="pre">numpy</span></code>. Using the NumPy library gives the generated function access to
powerful vectorized ufuncs that are backed by compiled C code.</p>
<p>Let us compare the speeds:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.autowrap</span> <span class="kn">import</span> <span class="n">ufuncify</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.lambdify</span> <span class="kn">import</span> <span class="n">lambdify</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fn_numpy</span> <span class="o">=</span> <span class="n">lambdify</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">expr</span><span class="p">,</span> <span class="s1">&#39;numpy&#39;</span><span class="p">)</span>   
<span class="gp">&gt;&gt;&gt; </span><span class="n">fn_fortran</span> <span class="o">=</span> <span class="n">ufuncify</span><span class="p">([</span><span class="n">x</span><span class="p">],</span> <span class="n">expr</span><span class="p">,</span> <span class="n">backend</span><span class="o">=</span><span class="s1">&#39;f2py&#39;</span><span class="p">)</span>    
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="n">linspace</span>  
<span class="gp">&gt;&gt;&gt; </span><span class="n">xx</span> <span class="o">=</span> <span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>  
<span class="gp">&gt;&gt;&gt; </span><span class="n">fn_numpy</span><span class="p">(</span><span class="n">xx</span><span class="p">)</span>    
<span class="go">[ 0.          1.21306132  0.98101184  0.44626032  0.        ]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fn_fortran</span><span class="p">(</span><span class="n">xx</span><span class="p">)</span>  
<span class="go">[ 0.          1.21306132  0.98101184  0.44626032  0.        ]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">timeit</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">timeit</span><span class="o">.</span><span class="n">timeit</span><span class="p">(</span><span class="s1">&#39;fn_numpy(xx)&#39;</span><span class="p">,</span> <span class="s1">&#39;from __main__ import fn_numpy, xx&#39;</span><span class="p">,</span> <span class="n">number</span><span class="o">=</span><span class="mi">10000</span><span class="p">)</span>    
<span class="go">0.18891601900395472</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">timeit</span><span class="o">.</span><span class="n">timeit</span><span class="p">(</span><span class="s1">&#39;fn_fortran(xx)&#39;</span><span class="p">,</span> <span class="s1">&#39;from __main__ import fn_fortran, xx&#39;</span><span class="p">,</span> <span class="n">number</span><span class="o">=</span><span class="mi">10000</span><span class="p">)</span>    
<span class="go">0.004707066000264604</span>
</pre></div>
</div>
<p>The options available with ufuncify are more or less the same as those
available with <code class="docutils literal notranslate"><span class="pre">autowrap</span></code>.</p>
<p>There are other facilities available with SymPy to do efficient numeric
computation. See <a class="reference internal" href="numeric-computation.html#numeric-computation"><span class="std std-ref">this</span></a> page for a comparison among them.</p>
</section>
<section id="module-sympy.codegen.rewriting">
<span id="classes-and-functions-for-rewriting-expressions-sympy-codegen-rewriting"></span><h2>Classes and functions for rewriting expressions (sympy.codegen.rewriting)<a class="headerlink" href="#module-sympy.codegen.rewriting" title="Permalink to this headline">¶</a></h2>
<p>Classes and functions useful for rewriting expressions for optimized code
generation. Some languages (or standards thereof), e.g. C99, offer specialized
math functions for better performance and/or precision.</p>
<p>Using the <code class="docutils literal notranslate"><span class="pre">optimize</span></code> function in this module, together with a collection of
rules (represented as instances of <code class="docutils literal notranslate"><span class="pre">Optimization</span></code>), one can rewrite the
expressions for this purpose:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span><span class="p">,</span> <span class="n">exp</span><span class="p">,</span> <span class="n">log</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.rewriting</span> <span class="kn">import</span> <span class="n">optimize</span><span class="p">,</span> <span class="n">optims_c99</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">optimize</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">)</span> <span class="o">-</span> <span class="mi">3</span><span class="p">,</span> <span class="n">optims_c99</span><span class="p">)</span>
<span class="go">3*expm1(2*x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">optimize</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mi">33</span><span class="p">),</span> <span class="n">optims_c99</span><span class="p">)</span>
<span class="go">expm1(2*x) - exp(-33)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">optimize</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">3</span><span class="p">),</span> <span class="n">optims_c99</span><span class="p">)</span>
<span class="go">log1p(x) + log(3)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">optimize</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">3</span><span class="p">),</span> <span class="n">optims_c99</span><span class="p">)</span>
<span class="go">log(2*x + 3)</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">optims_c99</span></code> imported above is tuple containing the following instances
(which may be imported from <code class="docutils literal notranslate"><span class="pre">sympy.codegen.rewriting</span></code>):</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">expm1_opt</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">log1p_opt</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">exp2_opt</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">log2_opt</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">log2const_opt</span></code></p></li>
</ul>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.rewriting.FuncMinusOneOptim">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.rewriting.</span></span><span class="sig-name descname"><span class="pre">FuncMinusOneOptim</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">func_m_1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">opportunistic</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/rewriting.py#L178-L266"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.rewriting.FuncMinusOneOptim" title="Permalink to this definition">¶</a></dt>
<dd><p>Specialization of ReplaceOptim for functions evaluating “f(x) - 1”.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>func :</strong></p>
<blockquote>
<div><p>The function which is subtracted by one.</p>
</div></blockquote>
<p><strong>func_m_1 :</strong></p>
<blockquote>
<div><p>The specialized function evaluating <code class="docutils literal notranslate"><span class="pre">func(x)</span> <span class="pre">-</span> <span class="pre">1</span></code>.</p>
</div></blockquote>
<p><strong>opportunistic</strong> : bool</p>
<blockquote>
<div><p>When <code class="docutils literal notranslate"><span class="pre">True</span></code>, apply the transformation as long as the magnitude of the
remaining number terms decreases. When <code class="docutils literal notranslate"><span class="pre">False</span></code>, only apply the
transformation if it completely eliminates the number term.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Explanation</p>
<p>Numerical functions which go toward one as x go toward zero is often best
implemented by a dedicated function in order to avoid catastrophic
cancellation. One such example is <code class="docutils literal notranslate"><span class="pre">expm1(x)</span></code> in the C standard library
which evaluates <code class="docutils literal notranslate"><span class="pre">exp(x)</span> <span class="pre">-</span> <span class="pre">1</span></code>. Such functions preserves many more
significant digits when its argument is much smaller than one, compared
to subtracting one afterwards.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span><span class="p">,</span> <span class="n">exp</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.rewriting</span> <span class="kn">import</span> <span class="n">FuncMinusOneOptim</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.cfunctions</span> <span class="kn">import</span> <span class="n">expm1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expm1_opt</span> <span class="o">=</span> <span class="n">FuncMinusOneOptim</span><span class="p">(</span><span class="n">exp</span><span class="p">,</span> <span class="n">expm1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expm1_opt</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="mi">5</span><span class="o">*</span><span class="n">y</span><span class="p">)</span> <span class="o">-</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">expm1(x) + 2*expm1(5*y)</span>
</pre></div>
</div>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.rewriting.FuncMinusOneOptim.replace_in_Add">
<span class="sig-name descname"><span class="pre">replace_in_Add</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">e</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/rewriting.py#L231-L261"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.rewriting.FuncMinusOneOptim.replace_in_Add" title="Permalink to this definition">¶</a></dt>
<dd><p>passed as second argument to Basic.replace(…)</p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.rewriting.Optimization">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.rewriting.</span></span><span class="sig-name descname"><span class="pre">Optimization</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cost_function</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">priority</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/rewriting.py#L46-L63"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.rewriting.Optimization" title="Permalink to this definition">¶</a></dt>
<dd><p>Abstract base class for rewriting optimization.</p>
<p>Subclasses should implement <code class="docutils literal notranslate"><span class="pre">__call__</span></code> taking an expression
as argument.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>cost_function</strong> : callable returning number</p>
<p><strong>priority</strong> : number</p>
</dd>
</dl>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.rewriting.ReplaceOptim">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.rewriting.</span></span><span class="sig-name descname"><span class="pre">ReplaceOptim</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">query</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/rewriting.py#L66-L104"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.rewriting.ReplaceOptim" title="Permalink to this definition">¶</a></dt>
<dd><p>Rewriting optimization calling replace on expressions.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>query :</strong></p>
<blockquote>
<div><p>First argument passed to replace.</p>
</div></blockquote>
<p><strong>value :</strong></p>
<blockquote>
<div><p>Second argument passed to replace.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Explanation</p>
<p>The instance can be used as a function on expressions for which
it will apply the <code class="docutils literal notranslate"><span class="pre">replace</span></code> method (see
<a class="reference internal" href="core.html#sympy.core.basic.Basic.replace" title="sympy.core.basic.Basic.replace"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sympy.core.basic.Basic.replace()</span></code></a>).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.rewriting</span> <span class="kn">import</span> <span class="n">ReplaceOptim</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.cfunctions</span> <span class="kn">import</span> <span class="n">exp2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">exp2_opt</span> <span class="o">=</span> <span class="n">ReplaceOptim</span><span class="p">(</span><span class="k">lambda</span> <span class="n">p</span><span class="p">:</span> <span class="n">p</span><span class="o">.</span><span class="n">is_Pow</span> <span class="ow">and</span> <span class="n">p</span><span class="o">.</span><span class="n">base</span> <span class="o">==</span> <span class="mi">2</span><span class="p">,</span>
<span class="gp">... </span>    <span class="k">lambda</span> <span class="n">p</span><span class="p">:</span> <span class="n">exp2</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">exp</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">exp2_opt</span><span class="p">(</span><span class="mi">2</span><span class="o">**</span><span class="n">x</span><span class="p">)</span>
<span class="go">exp2(x)</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.codegen.rewriting.create_expand_pow_optimization">
<span class="sig-prename descclassname"><span class="pre">sympy.codegen.rewriting.</span></span><span class="sig-name descname"><span class="pre">create_expand_pow_optimization</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">limit</span></em>, <em class="sig-param"><span class="pre">*</span></em>, <em class="sig-param"><span class="pre">base_req=&lt;function</span> <span class="pre">&lt;lambda&gt;&gt;</span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/rewriting.py#L279-L319"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.rewriting.create_expand_pow_optimization" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates an instance of <a class="reference internal" href="#sympy.codegen.rewriting.ReplaceOptim" title="sympy.codegen.rewriting.ReplaceOptim"><code class="xref py py-class docutils literal notranslate"><span class="pre">ReplaceOptim</span></code></a> for expanding <code class="docutils literal notranslate"><span class="pre">Pow</span></code>.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>limit</strong> : int</p>
<blockquote>
<div><p>The highest power which is expanded into multiplication.</p>
</div></blockquote>
<p><strong>base_req</strong> : function returning bool</p>
<blockquote>
<div><p>Requirement on base for expansion to happen, default is to return
the <code class="docutils literal notranslate"><span class="pre">is_symbol</span></code> attribute of the base.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Explanation</p>
<p>The requirements for expansions are that the base needs to be a symbol
and the exponent needs to be an Integer (and be less than or equal to
<code class="docutils literal notranslate"><span class="pre">limit</span></code>).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span><span class="p">,</span> <span class="n">sin</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.rewriting</span> <span class="kn">import</span> <span class="n">create_expand_pow_optimization</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expand_opt</span> <span class="o">=</span> <span class="n">create_expand_pow_optimization</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expand_opt</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">5</span> <span class="o">+</span> <span class="n">x</span><span class="o">**</span><span class="mi">3</span><span class="p">)</span>
<span class="go">x**5 + x*x*x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expand_opt</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">5</span> <span class="o">+</span> <span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">+</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">3</span><span class="p">)</span>
<span class="go">x**5 + sin(x)**3 + x*x*x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">opt2</span> <span class="o">=</span> <span class="n">create_expand_pow_optimization</span><span class="p">(</span><span class="mi">3</span> <span class="p">,</span> <span class="n">base_req</span><span class="o">=</span><span class="k">lambda</span> <span class="n">b</span><span class="p">:</span> <span class="ow">not</span> <span class="n">b</span><span class="o">.</span><span class="n">is_Function</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">opt2</span><span class="p">((</span><span class="n">x</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">sin(x)**2 + (x + 1)*(x + 1)</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.codegen.rewriting.optimize">
<span class="sig-prename descclassname"><span class="pre">sympy.codegen.rewriting.</span></span><span class="sig-name descname"><span class="pre">optimize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optimizations</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/rewriting.py#L107-L134"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.rewriting.optimize" title="Permalink to this definition">¶</a></dt>
<dd><p>Apply optimizations to an expression.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>expr</strong> : expression</p>
<p><strong>optimizations</strong> : iterable of <code class="docutils literal notranslate"><span class="pre">Optimization</span></code> instances</p>
<blockquote>
<div><p>The optimizations will be sorted with respect to <code class="docutils literal notranslate"><span class="pre">priority</span></code> (highest first).</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">log</span><span class="p">,</span> <span class="n">Symbol</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.rewriting</span> <span class="kn">import</span> <span class="n">optims_c99</span><span class="p">,</span> <span class="n">optimize</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">optimize</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">x</span><span class="o">+</span><span class="mi">3</span><span class="p">)</span><span class="o">/</span><span class="n">log</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="n">log</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">),</span> <span class="n">optims_c99</span><span class="p">)</span>
<span class="go">log1p(x**2) + log2(x + 3)</span>
</pre></div>
</div>
</dd></dl>

<span class="target" id="module-sympy.codegen.matrix_nodes"></span><p>Additional AST nodes for operations on matrices. The nodes in this module
are meant to represent optimization of matrix expressions within codegen’s
target languages that cannot be represented by SymPy expressions.</p>
<p>As an example, we can use <a class="reference internal" href="#sympy.codegen.rewriting.optimize" title="sympy.codegen.rewriting.optimize"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sympy.codegen.rewriting.optimize()</span></code></a> and the
<code class="docutils literal notranslate"><span class="pre">matin_opt</span></code> optimization provided in <a class="reference internal" href="#module-sympy.codegen.rewriting" title="sympy.codegen.rewriting"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sympy.codegen.rewriting</span></code></a> to
transform matrix multiplication under certain assumptions:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span><span class="p">,</span> <span class="n">MatrixSymbol</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;n&#39;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span> <span class="o">=</span> <span class="n">MatrixSymbol</span><span class="p">(</span><span class="s1">&#39;A&#39;</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">MatrixSymbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">A</span><span class="o">**</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.assumptions</span> <span class="kn">import</span> <span class="n">assuming</span><span class="p">,</span> <span class="n">Q</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.rewriting</span> <span class="kn">import</span> <span class="n">matinv_opt</span><span class="p">,</span> <span class="n">optimize</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">assuming</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">fullrank</span><span class="p">(</span><span class="n">A</span><span class="p">)):</span>
<span class="gp">... </span>    <span class="n">optimize</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="p">[</span><span class="n">matinv_opt</span><span class="p">])</span>
<span class="go">MatrixSolve(A, vector=x)</span>
</pre></div>
</div>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.matrix_nodes.MatrixSolve">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.matrix_nodes.</span></span><span class="sig-name descname"><span class="pre">MatrixSolve</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/matrix_nodes.py#L27-L66"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.matrix_nodes.MatrixSolve" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents an operation to solve a linear matrix equation.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>matrix</strong> : MatrixSymbol</p>
<blockquote>
<div><p>Matrix representing the coefficients of variables in the linear
equation. This matrix must be square and full-rank (i.e. all columns must
be linearly independent) for the solving operation to be valid.</p>
</div></blockquote>
<p><strong>vector</strong> : MatrixSymbol</p>
<blockquote>
<div><p>One-column matrix representing the solutions to the equations
represented in <code class="docutils literal notranslate"><span class="pre">matrix</span></code>.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span><span class="p">,</span> <span class="n">MatrixSymbol</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.matrix_nodes</span> <span class="kn">import</span> <span class="n">MatrixSolve</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;n&#39;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span> <span class="o">=</span> <span class="n">MatrixSymbol</span><span class="p">(</span><span class="s1">&#39;A&#39;</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">MatrixSymbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing.numpy</span> <span class="kn">import</span> <span class="n">NumPyPrinter</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">NumPyPrinter</span><span class="p">()</span><span class="o">.</span><span class="n">doprint</span><span class="p">(</span><span class="n">MatrixSolve</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">x</span><span class="p">))</span>
<span class="go">&#39;numpy.linalg.solve(A, x)&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">octave_code</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">octave_code</span><span class="p">(</span><span class="n">MatrixSolve</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">x</span><span class="p">))</span>
<span class="go">&#39;A \\ x&#39;</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="module-sympy.codegen.approximations">
<span id="tools-for-simplifying-expressions-using-approximations-sympy-codegen-approximations"></span><h2>Tools for simplifying expressions using approximations (sympy.codegen.approximations)<a class="headerlink" href="#module-sympy.codegen.approximations" title="Permalink to this headline">¶</a></h2>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.approximations.SeriesApprox">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.approximations.</span></span><span class="sig-name descname"><span class="pre">SeriesApprox</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">bounds</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">reltol</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">max_order</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">4</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n_point_checks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">4</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/approximations.py#L103-L187"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.approximations.SeriesApprox" title="Permalink to this definition">¶</a></dt>
<dd><p>Approximates functions by expanding them as a series.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>bounds</strong> : dict</p>
<blockquote>
<div><p>Mapping expressions to length 2 tuple of bounds (low, high).</p>
</div></blockquote>
<p><strong>reltol</strong> : number</p>
<blockquote>
<div><p>Threshold for when to ignore a term. Taken relative to the largest
lower bound among bounds.</p>
</div></blockquote>
<p><strong>max_order</strong> : int</p>
<blockquote>
<div><p>Largest order to include in series expansion</p>
</div></blockquote>
<p><strong>n_point_checks</strong> : int (even)</p>
<blockquote>
<div><p>The validity of an expansion (with respect to reltol) is checked at
discrete points (linearly spaced over the bounds of the variable). The
number of points used in this numerical check is given by this number.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sin</span><span class="p">,</span> <span class="n">pi</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.rewriting</span> <span class="kn">import</span> <span class="n">optimize</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.approximations</span> <span class="kn">import</span> <span class="n">SeriesApprox</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">bounds</span> <span class="o">=</span> <span class="p">{</span><span class="n">x</span><span class="p">:</span> <span class="p">(</span><span class="o">-</span><span class="mf">.1</span><span class="p">,</span> <span class="mf">.1</span><span class="p">),</span> <span class="n">y</span><span class="p">:</span> <span class="p">(</span><span class="n">pi</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">pi</span><span class="o">+</span><span class="mi">1</span><span class="p">)}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">series_approx2</span> <span class="o">=</span> <span class="n">SeriesApprox</span><span class="p">(</span><span class="n">bounds</span><span class="p">,</span> <span class="n">reltol</span><span class="o">=</span><span class="mf">1e-2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">series_approx3</span> <span class="o">=</span> <span class="n">SeriesApprox</span><span class="p">(</span><span class="n">bounds</span><span class="p">,</span> <span class="n">reltol</span><span class="o">=</span><span class="mf">1e-3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">series_approx8</span> <span class="o">=</span> <span class="n">SeriesApprox</span><span class="p">(</span><span class="n">bounds</span><span class="p">,</span> <span class="n">reltol</span><span class="o">=</span><span class="mf">1e-8</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">sin</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">optimize</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="p">[</span><span class="n">series_approx2</span><span class="p">])</span>
<span class="go">x*(-y + (y - pi)**3/6 + pi)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">optimize</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="p">[</span><span class="n">series_approx3</span><span class="p">])</span>
<span class="go">(-x**3/6 + x)*sin(y)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">optimize</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="p">[</span><span class="n">series_approx8</span><span class="p">])</span>
<span class="go">sin(x)*sin(y)</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.approximations.SumApprox">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.approximations.</span></span><span class="sig-name descname"><span class="pre">SumApprox</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">bounds</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">reltol</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/approximations.py#L14-L100"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.approximations.SumApprox" title="Permalink to this definition">¶</a></dt>
<dd><p>Approximates sum by neglecting small terms.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>bounds</strong> : dict</p>
<blockquote>
<div><p>Mapping expressions to length 2 tuple of bounds (low, high).</p>
</div></blockquote>
<p><strong>reltol</strong> : number</p>
<blockquote>
<div><p>Threshold for when to ignore a term. Taken relative to the largest
lower bound among bounds.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Explanation</p>
<p>If terms are expressions which can be determined to be monotonic, then
bounds for those expressions are added.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">exp</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.rewriting</span> <span class="kn">import</span> <span class="n">optimize</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.approximations</span> <span class="kn">import</span> <span class="n">SumApprox</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">bounds</span> <span class="o">=</span> <span class="p">{</span><span class="n">x</span><span class="p">:</span> <span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">y</span><span class="p">:</span> <span class="p">(</span><span class="mi">1000</span><span class="p">,</span> <span class="mi">2000</span><span class="p">),</span> <span class="n">z</span><span class="p">:</span> <span class="p">(</span><span class="o">-</span><span class="mi">10</span><span class="p">,</span> <span class="mi">3</span><span class="p">)}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sum_approx3</span> <span class="o">=</span> <span class="n">SumApprox</span><span class="p">(</span><span class="n">bounds</span><span class="p">,</span> <span class="n">reltol</span><span class="o">=</span><span class="mf">1e-3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sum_approx2</span> <span class="o">=</span> <span class="n">SumApprox</span><span class="p">(</span><span class="n">bounds</span><span class="p">,</span> <span class="n">reltol</span><span class="o">=</span><span class="mf">1e-2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sum_approx1</span> <span class="o">=</span> <span class="n">SumApprox</span><span class="p">(</span><span class="n">bounds</span><span class="p">,</span> <span class="n">reltol</span><span class="o">=</span><span class="mf">1e-1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="mi">3</span><span class="o">*</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o">+</span> <span class="n">exp</span><span class="p">(</span><span class="n">z</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">optimize</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="p">[</span><span class="n">sum_approx3</span><span class="p">])</span>
<span class="go">3*(x + y + exp(z))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">optimize</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="p">[</span><span class="n">sum_approx2</span><span class="p">])</span>
<span class="go">3*y + 3*exp(z)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">optimize</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="p">[</span><span class="n">sum_approx1</span><span class="p">])</span>
<span class="go">3*y</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="module-sympy.codegen.ast">
<span id="classes-for-abstract-syntax-trees-sympy-codegen-ast"></span><h2>Classes for abstract syntax trees (sympy.codegen.ast)<a class="headerlink" href="#module-sympy.codegen.ast" title="Permalink to this headline">¶</a></h2>
<p>Types used to represent a full function/module as an Abstract Syntax Tree.</p>
<p>Most types are small, and are merely used as tokens in the AST. A tree diagram
has been included below to illustrate the relationships between the AST types.</p>
<section id="ast-type-tree">
<h3>AST Type Tree<a class="headerlink" href="#ast-type-tree" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">*</span><span class="n">Basic</span><span class="o">*</span>
     <span class="o">|---&gt;</span><span class="n">AssignmentBase</span>
     <span class="o">|</span>             <span class="o">|---&gt;</span><span class="n">Assignment</span>
     <span class="o">|</span>             <span class="o">|---&gt;</span><span class="n">AugmentedAssignment</span>
     <span class="o">|</span>                                    <span class="o">|---&gt;</span><span class="n">AddAugmentedAssignment</span>
     <span class="o">|</span>                                    <span class="o">|---&gt;</span><span class="n">SubAugmentedAssignment</span>
     <span class="o">|</span>                                    <span class="o">|---&gt;</span><span class="n">MulAugmentedAssignment</span>
     <span class="o">|</span>                                    <span class="o">|---&gt;</span><span class="n">DivAugmentedAssignment</span>
     <span class="o">|</span>                                    <span class="o">|---&gt;</span><span class="n">ModAugmentedAssignment</span>
     <span class="o">|</span>
     <span class="o">|---&gt;</span><span class="n">CodeBlock</span>
     <span class="o">|</span>
     <span class="o">|</span>
     <span class="o">|---&gt;</span><span class="n">Token</span>
     <span class="o">|</span>        <span class="o">|---&gt;</span><span class="n">Attribute</span>
     <span class="o">|</span>        <span class="o">|---&gt;</span><span class="n">For</span>
     <span class="o">|</span>        <span class="o">|---&gt;</span><span class="n">String</span>
     <span class="o">|</span>        <span class="o">|</span>       <span class="o">|---&gt;</span><span class="n">QuotedString</span>
     <span class="o">|</span>        <span class="o">|</span>       <span class="o">|---&gt;</span><span class="n">Comment</span>
     <span class="o">|</span>        <span class="o">|---&gt;</span><span class="n">Type</span>
     <span class="o">|</span>        <span class="o">|</span>       <span class="o">|---&gt;</span><span class="n">IntBaseType</span>
     <span class="o">|</span>        <span class="o">|</span>       <span class="o">|</span>              <span class="o">|---&gt;</span><span class="n">_SizedIntType</span>
     <span class="o">|</span>        <span class="o">|</span>       <span class="o">|</span>                               <span class="o">|---&gt;</span><span class="n">SignedIntType</span>
     <span class="o">|</span>        <span class="o">|</span>       <span class="o">|</span>                               <span class="o">|---&gt;</span><span class="n">UnsignedIntType</span>
     <span class="o">|</span>        <span class="o">|</span>       <span class="o">|---&gt;</span><span class="n">FloatBaseType</span>
     <span class="o">|</span>        <span class="o">|</span>                        <span class="o">|---&gt;</span><span class="n">FloatType</span>
     <span class="o">|</span>        <span class="o">|</span>                        <span class="o">|---&gt;</span><span class="n">ComplexBaseType</span>
     <span class="o">|</span>        <span class="o">|</span>                                           <span class="o">|---&gt;</span><span class="n">ComplexType</span>
     <span class="o">|</span>        <span class="o">|---&gt;</span><span class="n">Node</span>
     <span class="o">|</span>        <span class="o">|</span>       <span class="o">|---&gt;</span><span class="n">Variable</span>
     <span class="o">|</span>        <span class="o">|</span>       <span class="o">|</span>           <span class="o">|---&gt;</span> <span class="n">Pointer</span>
     <span class="o">|</span>        <span class="o">|</span>       <span class="o">|---&gt;</span><span class="n">FunctionPrototype</span>
     <span class="o">|</span>        <span class="o">|</span>                            <span class="o">|---&gt;</span><span class="n">FunctionDefinition</span>
     <span class="o">|</span>        <span class="o">|---&gt;</span><span class="n">Element</span>
     <span class="o">|</span>        <span class="o">|---&gt;</span><span class="n">Declaration</span>
     <span class="o">|</span>        <span class="o">|---&gt;</span><span class="n">While</span>
     <span class="o">|</span>        <span class="o">|---&gt;</span><span class="n">Scope</span>
     <span class="o">|</span>        <span class="o">|---&gt;</span><span class="n">Stream</span>
     <span class="o">|</span>        <span class="o">|---&gt;</span><span class="n">Print</span>
     <span class="o">|</span>        <span class="o">|---&gt;</span><span class="n">FunctionCall</span>
     <span class="o">|</span>        <span class="o">|---&gt;</span><span class="n">BreakToken</span>
     <span class="o">|</span>        <span class="o">|---&gt;</span><span class="n">ContinueToken</span>
     <span class="o">|</span>        <span class="o">|---&gt;</span><span class="n">NoneToken</span>
     <span class="o">|</span>
     <span class="o">|---&gt;</span><span class="n">Statement</span>
     <span class="o">|---&gt;</span><span class="n">Return</span>
</pre></div>
</div>
</section>
<section id="predefined-types">
<h3>Predefined types<a class="headerlink" href="#predefined-types" title="Permalink to this headline">¶</a></h3>
<p>A number of <code class="docutils literal notranslate"><span class="pre">Type</span></code> instances are provided in the <code class="docutils literal notranslate"><span class="pre">sympy.codegen.ast</span></code> module
for convenience. Perhaps the two most common ones for code-generation (of numeric
codes) are <code class="docutils literal notranslate"><span class="pre">float32</span></code> and <code class="docutils literal notranslate"><span class="pre">float64</span></code> (known as single and double precision respectively).
There are also precision generic versions of Types (for which the codeprinters selects the
underlying data type at time of printing): <code class="docutils literal notranslate"><span class="pre">real</span></code>, <code class="docutils literal notranslate"><span class="pre">integer</span></code>, <code class="docutils literal notranslate"><span class="pre">complex_</span></code>, <code class="docutils literal notranslate"><span class="pre">bool_</span></code>.</p>
<p>The other <code class="docutils literal notranslate"><span class="pre">Type</span></code> instances defined are:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">intc</span></code>: Integer type used by C’s “int”.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">intp</span></code>: Integer type used by C’s “unsigned”.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">int8</span></code>, <code class="docutils literal notranslate"><span class="pre">int16</span></code>, <code class="docutils literal notranslate"><span class="pre">int32</span></code>, <code class="docutils literal notranslate"><span class="pre">int64</span></code>: n-bit integers.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">uint8</span></code>, <code class="docutils literal notranslate"><span class="pre">uint16</span></code>, <code class="docutils literal notranslate"><span class="pre">uint32</span></code>, <code class="docutils literal notranslate"><span class="pre">uint64</span></code>: n-bit unsigned integers.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">float80</span></code>: known as “extended precision” on modern x86/amd64 hardware.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">complex64</span></code>: Complex number represented by two <code class="docutils literal notranslate"><span class="pre">float32</span></code> numbers</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">complex128</span></code>: Complex number represented by two <code class="docutils literal notranslate"><span class="pre">float64</span></code> numbers</p></li>
</ul>
</section>
<section id="using-the-nodes">
<h3>Using the nodes<a class="headerlink" href="#using-the-nodes" title="Permalink to this headline">¶</a></h3>
<p>It is possible to construct simple algorithms using the AST nodes. Let’s construct a loop applying
Newton’s method:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span><span class="p">,</span> <span class="n">cos</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">While</span><span class="p">,</span> <span class="n">Assignment</span><span class="p">,</span> <span class="n">aug_assign</span><span class="p">,</span> <span class="n">Print</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="p">,</span> <span class="n">dx</span><span class="p">,</span> <span class="n">x</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;tol delta val&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">-</span> <span class="n">x</span><span class="o">**</span><span class="mi">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">whl</span> <span class="o">=</span> <span class="n">While</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">dx</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">t</span><span class="p">,</span> <span class="p">[</span>
<span class="gp">... </span>    <span class="n">Assignment</span><span class="p">(</span><span class="n">dx</span><span class="p">,</span> <span class="o">-</span><span class="n">expr</span><span class="o">/</span><span class="n">expr</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">x</span><span class="p">)),</span>
<span class="gp">... </span>    <span class="n">aug_assign</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="s1">&#39;+&#39;</span><span class="p">,</span> <span class="n">dx</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">Print</span><span class="p">([</span><span class="n">x</span><span class="p">])</span>
<span class="gp">... </span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">pycode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">py_str</span> <span class="o">=</span> <span class="n">pycode</span><span class="p">(</span><span class="n">whl</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">py_str</span><span class="p">)</span>
<span class="go">while (abs(delta) &gt; tol):</span>
<span class="go">    delta = (val**3 - math.cos(val))/(-3*val**2 - math.sin(val))</span>
<span class="go">    val += delta</span>
<span class="go">    print(val)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">math</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tol</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="n">delta</span> <span class="o">=</span> <span class="mf">1e-5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="nb">float</span><span class="p">(</span><span class="s1">&#39;inf&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">exec</span><span class="p">(</span><span class="n">py_str</span><span class="p">)</span>
<span class="go">1.1121416371</span>
<span class="go">0.909672693737</span>
<span class="go">0.867263818209</span>
<span class="go">0.865477135298</span>
<span class="go">0.865474033111</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%3.1g</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="o">-</span> <span class="n">val</span><span class="o">**</span><span class="mi">3</span><span class="p">))</span>
<span class="go">-3e-11</span>
</pre></div>
</div>
<p>If we want to generate Fortran code for the same while loop we simple call <code class="docutils literal notranslate"><span class="pre">fcode</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fcode</span><span class="p">(</span><span class="n">whl</span><span class="p">,</span> <span class="n">standard</span><span class="o">=</span><span class="mi">2003</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">))</span>
<span class="go">do while (abs(delta) &gt; tol)</span>
<span class="go">   delta = (val**3 - cos(val))/(-3*val**2 - sin(val))</span>
<span class="go">   val = val + delta</span>
<span class="go">   print *, val</span>
<span class="go">end do</span>
</pre></div>
</div>
<p>There is a function constructing a loop (or a complete function) like this in
<a class="reference internal" href="#module-sympy.codegen.algorithms" title="sympy.codegen.algorithms"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sympy.codegen.algorithms</span></code></a>.</p>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.Assignment">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">Assignment</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">lhs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">rhs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L453-L490"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Assignment" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents variable assignment for code generation.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>lhs</strong> : Expr</p>
<blockquote>
<div><p>Sympy object representing the lhs of the expression. These should be
singular objects, such as one would use in writing code. Notable types
include Symbol, MatrixSymbol, MatrixElement, and Indexed. Types that
subclass these types are also supported.</p>
</div></blockquote>
<p><strong>rhs</strong> : Expr</p>
<blockquote>
<div><p>Sympy object representing the rhs of the expression. This can be any
type, provided its shape corresponds to that of the lhs. For example,
a Matrix type can be assigned to MatrixSymbol, but not to Symbol, as
the dimensions will not align.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span><span class="p">,</span> <span class="n">MatrixSymbol</span><span class="p">,</span> <span class="n">Matrix</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Assignment</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x, y, z&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Assignment</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="go">Assignment(x, y)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Assignment</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="go">Assignment(x, 0)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span> <span class="o">=</span> <span class="n">MatrixSymbol</span><span class="p">(</span><span class="s1">&#39;A&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mat</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">])</span><span class="o">.</span><span class="n">T</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Assignment</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">mat</span><span class="p">)</span>
<span class="go">Assignment(A, Matrix([[x, y, z]]))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Assignment</span><span class="p">(</span><span class="n">A</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="n">x</span><span class="p">)</span>
<span class="go">Assignment(A[0, 1], x)</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.AssignmentBase">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">AssignmentBase</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">lhs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">rhs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L396-L450"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.AssignmentBase" title="Permalink to this definition">¶</a></dt>
<dd><p>Abstract base class for Assignment and AugmentedAssignment.</p>
<p class="rubric">Attributes:</p>
<dl class="simple">
<dt>op<span class="classifier">str</span></dt><dd><p>Symbol for assignment operator, e.g. “=”, “+=”, etc.</p>
</dd>
</dl>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.Attribute">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">Attribute</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">possibly</span> <span class="pre">parametrized</span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1334-L1372"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Attribute" title="Permalink to this definition">¶</a></dt>
<dd><p>For use with <a class="reference internal" href="#sympy.codegen.ast.Node" title="sympy.codegen.ast.Node"><code class="xref py py-class docutils literal notranslate"><span class="pre">sympy.codegen.ast.Node</span></code></a> (which takes instances of
<code class="docutils literal notranslate"><span class="pre">Attribute</span></code> as <code class="docutils literal notranslate"><span class="pre">attrs</span></code>).</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>name</strong> : str</p>
<p><strong>parameters</strong> : Tuple</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Attribute</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">volatile</span> <span class="o">=</span> <span class="n">Attribute</span><span class="p">(</span><span class="s1">&#39;volatile&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">volatile</span>
<span class="go">volatile</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">volatile</span><span class="p">))</span>
<span class="go">Attribute(String(&#39;volatile&#39;))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Attribute</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">foo(1, 2, 3)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">parameters</span> <span class="o">==</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.AugmentedAssignment">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">AugmentedAssignment</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">lhs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">rhs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L493-L508"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.AugmentedAssignment" title="Permalink to this definition">¶</a></dt>
<dd><p>Base class for augmented assignments.</p>
<p class="rubric">Attributes:</p>
<dl class="simple">
<dt>binop<span class="classifier">str</span></dt><dd><p>Symbol for binary operation being applied in the assignment, such as “+”,
“*”, etc.</p>
</dd>
</dl>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.BreakToken">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">BreakToken</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L332-L346"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.BreakToken" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents ‘break’ in C/Python (‘exit’ in Fortran).</p>
<p>Use the premade instance <code class="docutils literal notranslate"><span class="pre">break_</span></code> or instantiate manually.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">ccode</span><span class="p">,</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">break_</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ccode</span><span class="p">(</span><span class="n">break_</span><span class="p">)</span>
<span class="go">&#39;break&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fcode</span><span class="p">(</span><span class="n">break_</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">)</span>
<span class="go">&#39;exit&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.CodeBlock">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">CodeBlock</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L583-L802"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.CodeBlock" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a block of code.</p>
<p class="rubric">Explanation</p>
<p>For now only assignments are supported. This restriction will be lifted in
the future.</p>
<p>Useful attributes on this object are:</p>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">left_hand_sides</span></code>:</dt><dd><p>Tuple of left-hand sides of assignments, in order.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">left_hand_sides</span></code>:</dt><dd><p>Tuple of right-hand sides of assignments, in order.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">free_symbols</span></code>: Free symbols of the expressions in the right-hand sides</dt><dd><p>which do not appear in the left-hand side of an assignment.</p>
</dd>
</dl>
<p>Useful methods on this object are:</p>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">topological_sort</span></code>:</dt><dd><p>Class method. Return a CodeBlock with assignments
sorted so that variables are assigned before they
are used.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">cse</span></code>:</dt><dd><p>Return a new CodeBlock with common subexpressions eliminated and
pulled out as assignments.</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span><span class="p">,</span> <span class="n">ccode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">CodeBlock</span><span class="p">,</span> <span class="n">Assignment</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">CodeBlock</span><span class="p">(</span><span class="n">Assignment</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">Assignment</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ccode</span><span class="p">(</span><span class="n">c</span><span class="p">))</span>
<span class="go">x = 1;</span>
<span class="go">y = x + 1;</span>
</pre></div>
</div>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.ast.CodeBlock.cse">
<span class="sig-name descname"><span class="pre">cse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">symbols</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optimizations</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">postprocess</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">order</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'canonical'</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L740-L802"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.CodeBlock.cse" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a new code block with common subexpressions eliminated.</p>
<p class="rubric">Explanation</p>
<p>See the docstring of <a class="reference internal" href="simplify/simplify.html#sympy.simplify.cse_main.cse" title="sympy.simplify.cse_main.cse"><code class="xref py py-func docutils literal notranslate"><span class="pre">sympy.simplify.cse_main.cse()</span></code></a> for more
information.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span><span class="p">,</span> <span class="n">sin</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">CodeBlock</span><span class="p">,</span> <span class="n">Assignment</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y z&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">CodeBlock</span><span class="p">(</span>
<span class="gp">... </span>    <span class="n">Assignment</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">Assignment</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">Assignment</span><span class="p">(</span><span class="n">z</span><span class="p">,</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">),</span>
<span class="gp">... </span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">cse</span><span class="p">()</span>
<span class="go">CodeBlock(</span>
<span class="go">    Assignment(x, 1),</span>
<span class="go">    Assignment(x0, sin(x)),</span>
<span class="go">    Assignment(y, x0 + 1),</span>
<span class="go">    Assignment(z, x0 - 1)</span>
<span class="go">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.ast.CodeBlock.topological_sort">
<em class="property"><span class="pre">classmethod</span> </em><span class="sig-name descname"><span class="pre">topological_sort</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">assignments</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L655-L738"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.CodeBlock.topological_sort" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a CodeBlock with topologically sorted assignments so that
variables are assigned before they are used.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">CodeBlock</span><span class="p">,</span> <span class="n">Assignment</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y z&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">assignments</span> <span class="o">=</span> <span class="p">[</span>
<span class="gp">... </span>    <span class="n">Assignment</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">+</span> <span class="n">z</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">Assignment</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">+</span> <span class="mi">1</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">Assignment</span><span class="p">(</span><span class="n">z</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span>
<span class="gp">... </span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">CodeBlock</span><span class="o">.</span><span class="n">topological_sort</span><span class="p">(</span><span class="n">assignments</span><span class="p">)</span>
<span class="go">CodeBlock(</span>
<span class="go">    Assignment(z, 2),</span>
<span class="go">    Assignment(y, z + 1),</span>
<span class="go">    Assignment(x, y + z)</span>
<span class="go">)</span>
</pre></div>
</div>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.Comment">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">Comment</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L909-L910"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Comment" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a comment.</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.ComplexType">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">ComplexType</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1301-L1302"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.ComplexType" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a complex floating point number.</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.ContinueToken">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">ContinueToken</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L351-L365"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.ContinueToken" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents ‘continue’ in C/Python (‘cycle’ in Fortran)</p>
<p>Use the premade instance <code class="docutils literal notranslate"><span class="pre">continue_</span></code> or instantiate manually.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">ccode</span><span class="p">,</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">continue_</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ccode</span><span class="p">(</span><span class="n">continue_</span><span class="p">)</span>
<span class="go">&#39;continue&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fcode</span><span class="p">(</span><span class="n">continue_</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">)</span>
<span class="go">&#39;cycle&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.Declaration">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">Declaration</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1576-L1600"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Declaration" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a variable declaration</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>variable</strong> : Variable</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Declaration</span><span class="p">,</span> <span class="n">NoneToken</span><span class="p">,</span> <span class="n">untyped</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span> <span class="o">=</span> <span class="n">Declaration</span><span class="p">(</span><span class="s1">&#39;z&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span><span class="o">.</span><span class="n">variable</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="n">untyped</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># value is special NoneToken() which must be tested with == operator</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span><span class="o">.</span><span class="n">variable</span><span class="o">.</span><span class="n">value</span> <span class="ow">is</span> <span class="kc">None</span>  <span class="c1"># won&#39;t work</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span><span class="o">.</span><span class="n">variable</span><span class="o">.</span><span class="n">value</span> <span class="o">==</span> <span class="kc">None</span>  <span class="c1"># not PEP-8 compliant</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span><span class="o">.</span><span class="n">variable</span><span class="o">.</span><span class="n">value</span> <span class="o">==</span> <span class="n">NoneToken</span><span class="p">()</span>  <span class="c1"># OK</span>
<span class="go">True</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.Element">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">Element</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1549-L1573"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Element" title="Permalink to this definition">¶</a></dt>
<dd><p>Element in (a possibly N-dimensional) array.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Element</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">elem</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="s1">&#39;ijk&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">elem</span><span class="o">.</span><span class="n">symbol</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;x&#39;</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">elem</span><span class="o">.</span><span class="n">indices</span>
<span class="go">(i, j, k)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">ccode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ccode</span><span class="p">(</span><span class="n">elem</span><span class="p">)</span>
<span class="go">&#39;x[i][j][k]&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ccode</span><span class="p">(</span><span class="n">Element</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="s1">&#39;ijk&#39;</span><span class="p">,</span> <span class="n">strides</span><span class="o">=</span><span class="s1">&#39;lmn&#39;</span><span class="p">,</span> <span class="n">offset</span><span class="o">=</span><span class="s1">&#39;o&#39;</span><span class="p">))</span>
<span class="go">&#39;x[i*l + j*m + k*n + o]&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.FloatBaseType">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">FloatBaseType</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1163-L1165"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.FloatBaseType" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a floating point number type.</p>
<dl class="py attribute">
<dt class="sig sig-object py" id="sympy.codegen.ast.FloatBaseType.cast_nocheck">
<span class="sig-name descname"><span class="pre">cast_nocheck</span></span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/core/numbers.py#L855-L1482"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.FloatBaseType.cast_nocheck" title="Permalink to this definition">¶</a></dt>
<dd><p>alias of <a class="reference internal" href="core.html#sympy.core.numbers.Float" title="sympy.core.numbers.Float"><code class="xref py py-class docutils literal notranslate"><span class="pre">sympy.core.numbers.Float</span></code></a></p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.FloatType">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">FloatType</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1167-L1283"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.FloatType" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a floating point type with fixed bit width.</p>
<p>Base 2 &amp; one sign bit is assumed.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>name</strong> : str</p>
<blockquote>
<div><p>Name of the type.</p>
</div></blockquote>
<p><strong>nbits</strong> : integer</p>
<blockquote>
<div><p>Number of bits used (storage).</p>
</div></blockquote>
<p><strong>nmant</strong> : integer</p>
<blockquote>
<div><p>Number of bits used to represent the mantissa.</p>
</div></blockquote>
<p><strong>nexp</strong> : integer</p>
<blockquote>
<div><p>Number of bits used to represent the mantissa.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">S</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">FloatType</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">half_precision</span> <span class="o">=</span> <span class="n">FloatType</span><span class="p">(</span><span class="s1">&#39;f16&#39;</span><span class="p">,</span> <span class="n">nbits</span><span class="o">=</span><span class="mi">16</span><span class="p">,</span> <span class="n">nmant</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">nexp</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">half_precision</span><span class="o">.</span><span class="n">max</span>
<span class="go">65504</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">half_precision</span><span class="o">.</span><span class="n">tiny</span> <span class="o">==</span> <span class="n">S</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">**-</span><span class="mi">14</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">half_precision</span><span class="o">.</span><span class="n">eps</span> <span class="o">==</span> <span class="n">S</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">**-</span><span class="mi">10</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">half_precision</span><span class="o">.</span><span class="n">dig</span> <span class="o">==</span> <span class="mi">3</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">half_precision</span><span class="o">.</span><span class="n">decimal_dig</span> <span class="o">==</span> <span class="mi">5</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">half_precision</span><span class="o">.</span><span class="n">cast_check</span><span class="p">(</span><span class="mf">1.0</span><span class="p">)</span>
<span class="go">1.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">half_precision</span><span class="o">.</span><span class="n">cast_check</span><span class="p">(</span><span class="mf">1e5</span><span class="p">)</span>  
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">Maximum value for data type smaller than new value.</span>
</pre></div>
</div>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.ast.FloatType.cast_nocheck">
<span class="sig-name descname"><span class="pre">cast_nocheck</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1269-L1275"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.FloatType.cast_nocheck" title="Permalink to this definition">¶</a></dt>
<dd><p>Casts without checking if out of bounds or subnormal.</p>
</dd></dl>

<dl class="py property">
<dt class="sig sig-object py" id="sympy.codegen.ast.FloatType.decimal_dig">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">decimal_dig</span></span><a class="headerlink" href="#sympy.codegen.ast.FloatType.decimal_dig" title="Permalink to this definition">¶</a></dt>
<dd><p>Number of digits needed to store &amp; load without loss.</p>
<p class="rubric">Explanation</p>
<p>Number of decimal digits needed to guarantee that two consecutive conversions
(float -&gt; text -&gt; float) to be idempotent. This is useful when one do not want
to loose precision due to rounding errors when storing a floating point value
as text.</p>
</dd></dl>

<dl class="py property">
<dt class="sig sig-object py" id="sympy.codegen.ast.FloatType.dig">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">dig</span></span><a class="headerlink" href="#sympy.codegen.ast.FloatType.dig" title="Permalink to this definition">¶</a></dt>
<dd><p>Number of decimal digits that are guaranteed to be preserved in text.</p>
<p>When converting text -&gt; float -&gt; text, you are guaranteed that at least <code class="docutils literal notranslate"><span class="pre">dig</span></code>
number of digits are preserved with respect to rounding or overflow.</p>
</dd></dl>

<dl class="py property">
<dt class="sig sig-object py" id="sympy.codegen.ast.FloatType.eps">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">eps</span></span><a class="headerlink" href="#sympy.codegen.ast.FloatType.eps" title="Permalink to this definition">¶</a></dt>
<dd><p>Difference between 1.0 and the next representable value.</p>
</dd></dl>

<dl class="py property">
<dt class="sig sig-object py" id="sympy.codegen.ast.FloatType.max">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">max</span></span><a class="headerlink" href="#sympy.codegen.ast.FloatType.max" title="Permalink to this definition">¶</a></dt>
<dd><p>Maximum value representable.</p>
</dd></dl>

<dl class="py property">
<dt class="sig sig-object py" id="sympy.codegen.ast.FloatType.max_exponent">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">max_exponent</span></span><a class="headerlink" href="#sympy.codegen.ast.FloatType.max_exponent" title="Permalink to this definition">¶</a></dt>
<dd><p>The largest positive number n, such that 2**(n - 1) is a representable finite value.</p>
</dd></dl>

<dl class="py property">
<dt class="sig sig-object py" id="sympy.codegen.ast.FloatType.min_exponent">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">min_exponent</span></span><a class="headerlink" href="#sympy.codegen.ast.FloatType.min_exponent" title="Permalink to this definition">¶</a></dt>
<dd><p>The lowest negative number n, such that 2**(n - 1) is a valid normalized number.</p>
</dd></dl>

<dl class="py property">
<dt class="sig sig-object py" id="sympy.codegen.ast.FloatType.tiny">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">tiny</span></span><a class="headerlink" href="#sympy.codegen.ast.FloatType.tiny" title="Permalink to this definition">¶</a></dt>
<dd><p>The minimum positive normalized value.</p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.For">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">For</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L805-L864"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.For" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a ‘for-loop’ in the code.</p>
<blockquote>
<div><dl class="simple">
<dt>Expressions are of the form:</dt><dd><dl class="simple">
<dt>“for target in iter:</dt><dd><p>body…”</p>
</dd>
</dl>
</dd>
</dl>
</div></blockquote>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>target</strong> : symbol</p>
<blockquote>
<div><p>iter : iterable
body : CodeBlock or iterable</p>
</div></blockquote>
<p><strong>!        When passed an iterable it is used to instantiate a CodeBlock.</strong></p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span><span class="p">,</span> <span class="n">Range</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">aug_assign</span><span class="p">,</span> <span class="n">For</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="n">k</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x i j k&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">for_i</span> <span class="o">=</span> <span class="n">For</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">Range</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span> <span class="p">[</span><span class="n">aug_assign</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="s1">&#39;+&#39;</span><span class="p">,</span> <span class="n">i</span><span class="o">*</span><span class="n">j</span><span class="o">*</span><span class="n">k</span><span class="p">)])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">for_i</span>  
<span class="go">For(i, iterable=Range(0, 10, 1), body=CodeBlock(</span>
<span class="go">    AddAugmentedAssignment(x, i*j*k)</span>
<span class="go">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">for_ji</span> <span class="o">=</span> <span class="n">For</span><span class="p">(</span><span class="n">j</span><span class="p">,</span> <span class="n">Range</span><span class="p">(</span><span class="mi">7</span><span class="p">),</span> <span class="p">[</span><span class="n">for_i</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">for_ji</span>  
<span class="go">For(j, iterable=Range(0, 7, 1), body=CodeBlock(</span>
<span class="go">    For(i, iterable=Range(0, 10, 1), body=CodeBlock(</span>
<span class="go">        AddAugmentedAssignment(x, i*j*k)</span>
<span class="go">    ))</span>
<span class="go">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">for_kji</span> <span class="o">=</span><span class="n">For</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">Range</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span> <span class="p">[</span><span class="n">for_ji</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">for_kji</span>  
<span class="go">For(k, iterable=Range(0, 5, 1), body=CodeBlock(</span>
<span class="go">    For(j, iterable=Range(0, 7, 1), body=CodeBlock(</span>
<span class="go">        For(i, iterable=Range(0, 10, 1), body=CodeBlock(</span>
<span class="go">            AddAugmentedAssignment(x, i*j*k)</span>
<span class="go">        ))</span>
<span class="go">    ))</span>
<span class="go">))</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.FunctionCall">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">FunctionCall</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1818-L1840"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.FunctionCall" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a call to a function in the code.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>name</strong> : str</p>
<p><strong>function_args</strong> : Tuple</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">FunctionCall</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing.pycode</span> <span class="kn">import</span> <span class="n">pycode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fcall</span> <span class="o">=</span> <span class="n">FunctionCall</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="s1">&#39;bar baz&#39;</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">pycode</span><span class="p">(</span><span class="n">fcall</span><span class="p">))</span>
<span class="go">foo(bar, baz)</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.FunctionDefinition">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">FunctionDefinition</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1767-L1811"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.FunctionDefinition" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a function definition in the code.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>return_type</strong> : Type</p>
<p><strong>name</strong> : str</p>
<p><strong>parameters: iterable of Variable instances</strong></p>
<p><strong>body</strong> : CodeBlock or iterable</p>
<p><strong>attrs</strong> : iterable of Attribute instances</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">real</span><span class="p">,</span> <span class="n">FunctionPrototype</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">ccode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y&#39;</span><span class="p">,</span> <span class="n">real</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fp</span> <span class="o">=</span> <span class="n">FunctionPrototype</span><span class="p">(</span><span class="n">real</span><span class="p">,</span> <span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ccode</span><span class="p">(</span><span class="n">fp</span><span class="p">)</span>
<span class="go">&#39;double foo(double x, double y)&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">FunctionDefinition</span><span class="p">,</span> <span class="n">Return</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">body</span> <span class="o">=</span> <span class="p">[</span><span class="n">Return</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fd</span> <span class="o">=</span> <span class="n">FunctionDefinition</span><span class="o">.</span><span class="n">from_FunctionPrototype</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="n">body</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ccode</span><span class="p">(</span><span class="n">fd</span><span class="p">))</span>
<span class="go">double foo(double x, double y){</span>
<span class="go">    return x*y;</span>
<span class="go">}</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.FunctionPrototype">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">FunctionPrototype</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1718-L1764"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.FunctionPrototype" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a function prototype</p>
<p>Allows the user to generate forward declaration in e.g. C/C++.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>return_type</strong> : Type</p>
<p><strong>name</strong> : str</p>
<p><strong>parameters: iterable of Variable instances</strong></p>
<p><strong>attrs</strong> : iterable of Attribute instances</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">real</span><span class="p">,</span> <span class="n">FunctionPrototype</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">ccode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y&#39;</span><span class="p">,</span> <span class="n">real</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fp</span> <span class="o">=</span> <span class="n">FunctionPrototype</span><span class="p">(</span><span class="n">real</span><span class="p">,</span> <span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ccode</span><span class="p">(</span><span class="n">fp</span><span class="p">)</span>
<span class="go">&#39;double foo(double x, double y)&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.IntBaseType">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">IntBaseType</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1122-L1125"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.IntBaseType" title="Permalink to this definition">¶</a></dt>
<dd><p>Integer base type, contains no size information.</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.Node">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">Node</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L912-L943"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Node" title="Permalink to this definition">¶</a></dt>
<dd><p>Subclass of Token, carrying the attribute ‘attrs’ (Tuple)</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Node</span><span class="p">,</span> <span class="n">value_const</span><span class="p">,</span> <span class="n">pointer_const</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n1</span> <span class="o">=</span> <span class="n">Node</span><span class="p">([</span><span class="n">value_const</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n1</span><span class="o">.</span><span class="n">attr_params</span><span class="p">(</span><span class="s1">&#39;value_const&#39;</span><span class="p">)</span>  <span class="c1"># get the parameters of attribute (by name)</span>
<span class="go">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">dimension</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n2</span> <span class="o">=</span> <span class="n">Node</span><span class="p">([</span><span class="n">value_const</span><span class="p">,</span> <span class="n">dimension</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">3</span><span class="p">)])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n2</span><span class="o">.</span><span class="n">attr_params</span><span class="p">(</span><span class="n">value_const</span><span class="p">)</span>  <span class="c1"># get the parameters of attribute (by Attribute instance)</span>
<span class="go">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n2</span><span class="o">.</span><span class="n">attr_params</span><span class="p">(</span><span class="s1">&#39;dimension&#39;</span><span class="p">)</span>  <span class="c1"># get the parameters of attribute (by name)</span>
<span class="go">(5, 3)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n2</span><span class="o">.</span><span class="n">attr_params</span><span class="p">(</span><span class="n">pointer_const</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">None</span>
<span class="go">True</span>
</pre></div>
</div>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.ast.Node.attr_params">
<span class="sig-name descname"><span class="pre">attr_params</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">looking_for</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L939-L943"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Node.attr_params" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the parameters of the Attribute with name <code class="docutils literal notranslate"><span class="pre">looking_for</span></code> in self.attrs</p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.NoneToken">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">NoneToken</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L369-L390"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.NoneToken" title="Permalink to this definition">¶</a></dt>
<dd><p>The AST equivalence of Python’s NoneType</p>
<p>The corresponding instance of Python’s <code class="docutils literal notranslate"><span class="pre">None</span></code> is <code class="docutils literal notranslate"><span class="pre">none</span></code>.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">none</span><span class="p">,</span> <span class="n">Variable</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing.pycode</span> <span class="kn">import</span> <span class="n">pycode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">pycode</span><span class="p">(</span><span class="n">Variable</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">as_Declaration</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="n">none</span><span class="p">)))</span>
<span class="go">x = None</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.Pointer">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">Pointer</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1525-L1546"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Pointer" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a pointer. See <code class="docutils literal notranslate"><span class="pre">Variable</span></code>.</p>
<p class="rubric">Examples</p>
<p>Can create instances of <code class="docutils literal notranslate"><span class="pre">Element</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Pointer</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">i</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;i&#39;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Pointer</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span>
<span class="go">Element(x, indices=(i + 1,))</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.Print">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">Print</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1691-L1715"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Print" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents print command in the code.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>formatstring</strong> : str</p>
<p><strong>*args</strong> : Basic instances (or convertible to such through sympify)</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Print</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing.pycode</span> <span class="kn">import</span> <span class="n">pycode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">pycode</span><span class="p">(</span><span class="n">Print</span><span class="p">(</span><span class="s1">&#39;x y&#39;</span><span class="o">.</span><span class="n">split</span><span class="p">(),</span> <span class="s2">&quot;coordinate: </span><span class="si">%12.5g</span><span class="s2"> </span><span class="si">%12.5g</span><span class="s2">&quot;</span><span class="p">)))</span>
<span class="go">print(&quot;coordinate: %12.5g %12.5g&quot; % (x, y))</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.QuotedString">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">QuotedString</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L906-L907"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.QuotedString" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a string which should be printed with quotes.</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.Return">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">Return</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1814-L1815"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Return" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a return command in the code.</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.Scope">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">Scope</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1641-L1658"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Scope" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a scope in the code.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>body</strong> : CodeBlock or iterable</p>
<blockquote>
<div><p>When passed an iterable it is used to instantiate a CodeBlock.</p>
</div></blockquote>
</dd>
</dl>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.SignedIntType">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">SignedIntType</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1140-L1148"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.SignedIntType" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a signed integer type.</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.Stream">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">Stream</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1661-L1685"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Stream" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a stream.</p>
<p>There are two predefined Stream instances <code class="docutils literal notranslate"><span class="pre">stdout</span></code> &amp; <code class="docutils literal notranslate"><span class="pre">stderr</span></code>.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>name</strong> : str</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing.pycode</span> <span class="kn">import</span> <span class="n">pycode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Print</span><span class="p">,</span> <span class="n">stderr</span><span class="p">,</span> <span class="n">QuotedString</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">pycode</span><span class="p">(</span><span class="n">Print</span><span class="p">([</span><span class="s1">&#39;x&#39;</span><span class="p">],</span> <span class="n">file</span><span class="o">=</span><span class="n">stderr</span><span class="p">)))</span>
<span class="go">print(x, file=sys.stderr)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">pycode</span><span class="p">(</span><span class="n">Print</span><span class="p">([</span><span class="n">QuotedString</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)],</span> <span class="n">file</span><span class="o">=</span><span class="n">stderr</span><span class="p">)))</span>  <span class="c1"># print literally &quot;x&quot;</span>
<span class="go">print(&quot;x&quot;, file=sys.stderr)</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.String">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">String</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L867-L903"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.String" title="Permalink to this definition">¶</a></dt>
<dd><p>SymPy object representing a string.</p>
<p>Atomic object which is not an expression (as opposed to Symbol).</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>text</strong> : str</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">String</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">String</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span>
<span class="go">foo</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="go">&#39;foo&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">text</span>
<span class="go">&#39;foo&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">f</span><span class="p">))</span>
<span class="go">String(&#39;foo&#39;)</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.Token">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">Token</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L160-L329"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Token" title="Permalink to this definition">¶</a></dt>
<dd><p>Base class for the AST types.</p>
<p class="rubric">Explanation</p>
<p>Defining fields are set in <code class="docutils literal notranslate"><span class="pre">__slots__</span></code>. Attributes (defined in __slots__)
are only allowed to contain instances of Basic (unless atomic, see
<code class="docutils literal notranslate"><span class="pre">String</span></code>). The arguments to <code class="docutils literal notranslate"><span class="pre">__new__()</span></code> correspond to the attributes in
the order defined in <code class="docutils literal notranslate"><span class="pre">__slots__`.</span> <span class="pre">The</span> <span class="pre">``defaults</span></code> class attribute is a
dictionary mapping attribute names to their default values.</p>
<p>Subclasses should not need to override the <code class="docutils literal notranslate"><span class="pre">__new__()</span></code> method. They may
define a class or static method named <code class="docutils literal notranslate"><span class="pre">_construct_&lt;attr&gt;</span></code> for each
attribute to process the value passed to <code class="docutils literal notranslate"><span class="pre">__new__()</span></code>. Attributes listed
in the class attribute <code class="docutils literal notranslate"><span class="pre">not_in_args</span></code> are not passed to <a class="reference internal" href="core.html#sympy.core.basic.Basic" title="sympy.core.basic.Basic"><code class="xref py py-class docutils literal notranslate"><span class="pre">Basic</span></code></a>.</p>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.ast.Token.kwargs">
<span class="sig-name descname"><span class="pre">kwargs</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exclude</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">apply</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L313-L329"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Token.kwargs" title="Permalink to this definition">¶</a></dt>
<dd><p>Get instance’s attributes as dict of keyword arguments.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>exclude</strong> : collection of str</p>
<blockquote>
<div><p>Collection of keywords to exclude.</p>
</div></blockquote>
<p><strong>apply</strong> : callable, optional</p>
<blockquote>
<div><p>Function to apply to all values.</p>
</div></blockquote>
</dd>
</dl>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.Type">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">Type</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L946-L1119"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Type" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a type.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>name</strong> : str</p>
<blockquote>
<div><p>Name of the type, e.g. <code class="docutils literal notranslate"><span class="pre">object</span></code>, <code class="docutils literal notranslate"><span class="pre">int16</span></code>, <code class="docutils literal notranslate"><span class="pre">float16</span></code> (where the latter two
would use the <code class="docutils literal notranslate"><span class="pre">Type</span></code> sub-classes <code class="docutils literal notranslate"><span class="pre">IntType</span></code> and <code class="docutils literal notranslate"><span class="pre">FloatType</span></code> respectively).
If a <code class="docutils literal notranslate"><span class="pre">Type</span></code> instance is given, the said instance is returned.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Explanation</p>
<p>The naming is a super-set of NumPy naming. Type has a classmethod
<code class="docutils literal notranslate"><span class="pre">from_expr</span></code> which offer type deduction. It also has a method
<code class="docutils literal notranslate"><span class="pre">cast_check</span></code> which casts the argument to its type, possibly raising an
exception if rounding error is not within tolerances, or if the value is not
representable by the underlying data type (e.g. unsigned integers).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Type</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">Type</span><span class="o">.</span><span class="n">from_expr</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span>
<span class="go">integer</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">t</span><span class="p">))</span>
<span class="go">IntBaseType(String(&#39;integer&#39;))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">uint8</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">uint8</span><span class="o">.</span><span class="n">cast_check</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>   
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">Minimum value for data type bigger than new value.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">float32</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v6</span> <span class="o">=</span> <span class="mf">0.123456</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">float32</span><span class="o">.</span><span class="n">cast_check</span><span class="p">(</span><span class="n">v6</span><span class="p">)</span>
<span class="go">0.123456</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v10</span> <span class="o">=</span> <span class="mf">12345.67894</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">float32</span><span class="o">.</span><span class="n">cast_check</span><span class="p">(</span><span class="n">v10</span><span class="p">)</span>  
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">Casting gives a significantly different value.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">boost_mp50</span> <span class="o">=</span> <span class="n">Type</span><span class="p">(</span><span class="s1">&#39;boost::multiprecision::cpp_dec_float_50&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">cxxcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Declaration</span><span class="p">,</span> <span class="n">Variable</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cxxcode</span><span class="p">(</span><span class="n">Declaration</span><span class="p">(</span><span class="n">Variable</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="n">boost_mp50</span><span class="p">)))</span>
<span class="go">&#39;boost::multiprecision::cpp_dec_float_50 x&#39;</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r36"><span class="brackets"><a class="fn-backref" href="#id2">R36</a></span></dt>
<dd><p><a class="reference external" href="https://docs.scipy.org/doc/numpy/user/basics.types.html">https://docs.scipy.org/doc/numpy/user/basics.types.html</a></p>
</dd>
</dl>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.ast.Type.cast_check">
<span class="sig-name descname"><span class="pre">cast_check</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">value</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">rtol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">atol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">precision_targets</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1054-L1119"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Type.cast_check" title="Permalink to this definition">¶</a></dt>
<dd><p>Casts a value to the data type of the instance.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>value</strong> : number</p>
<p><strong>rtol</strong> : floating point number</p>
<blockquote>
<div><p>Relative tolerance. (will be deduced if not given).</p>
</div></blockquote>
<p><strong>atol</strong> : floating point number</p>
<blockquote>
<div><p>Absolute tolerance (in addition to <code class="docutils literal notranslate"><span class="pre">rtol</span></code>).</p>
</div></blockquote>
<p><strong>type_aliases</strong> : dict</p>
<blockquote>
<div><p>Maps substitutions for Type, e.g. {integer: int64, real: float32}</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">integer</span><span class="p">,</span> <span class="n">float32</span><span class="p">,</span> <span class="n">int8</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">integer</span><span class="o">.</span><span class="n">cast_check</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">float32</span><span class="o">.</span><span class="n">cast_check</span><span class="p">(</span><span class="mf">1e-40</span><span class="p">)</span>  
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">Minimum value for data type bigger than new value.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">int8</span><span class="o">.</span><span class="n">cast_check</span><span class="p">(</span><span class="mi">256</span><span class="p">)</span>  
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">Maximum value for data type smaller than new value.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v10</span> <span class="o">=</span> <span class="mf">12345.67894</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">float32</span><span class="o">.</span><span class="n">cast_check</span><span class="p">(</span><span class="n">v10</span><span class="p">)</span>  
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">Casting gives a significantly different value.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">float64</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">float64</span><span class="o">.</span><span class="n">cast_check</span><span class="p">(</span><span class="n">v10</span><span class="p">)</span>
<span class="go">12345.67894</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Float</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v18</span> <span class="o">=</span> <span class="n">Float</span><span class="p">(</span><span class="s1">&#39;0.123456789012345646&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">float64</span><span class="o">.</span><span class="n">cast_check</span><span class="p">(</span><span class="n">v18</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">Casting gives a significantly different value.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">float80</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">float80</span><span class="o">.</span><span class="n">cast_check</span><span class="p">(</span><span class="n">v18</span><span class="p">)</span>
<span class="go">0.123456789012345649</span>
</pre></div>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.ast.Type.from_expr">
<em class="property"><span class="pre">classmethod</span> </em><span class="sig-name descname"><span class="pre">from_expr</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1008-L1049"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Type.from_expr" title="Permalink to this definition">¶</a></dt>
<dd><p>Deduces type from an expression or a <code class="docutils literal notranslate"><span class="pre">Symbol</span></code>.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>expr</strong> : number or SymPy object</p>
<blockquote>
<div><p>The type will be deduced from type or properties.</p>
</div></blockquote>
</dd>
<dt class="field-even">Raises</dt>
<dd class="field-even"><p><strong>ValueError when type deduction fails.</strong></p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Type</span><span class="p">,</span> <span class="n">integer</span><span class="p">,</span> <span class="n">complex_</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Type</span><span class="o">.</span><span class="n">from_expr</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">==</span> <span class="n">integer</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Type</span><span class="o">.</span><span class="n">from_expr</span><span class="p">(</span><span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;z&#39;</span><span class="p">,</span> <span class="nb">complex</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span> <span class="o">==</span> <span class="n">complex_</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Type</span><span class="o">.</span><span class="n">from_expr</span><span class="p">(</span><span class="nb">sum</span><span class="p">)</span>  
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">Could not deduce type from expr.</span>
</pre></div>
</div>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.UnsignedIntType">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">UnsignedIntType</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1151-L1159"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.UnsignedIntType" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents an unsigned integer type.</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.Variable">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">Variable</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1378-L1523"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Variable" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a variable.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>symbol</strong> : Symbol</p>
<p><strong>type</strong> : Type (optional)</p>
<blockquote>
<div><p>Type of the variable.</p>
</div></blockquote>
<p><strong>attrs</strong> : iterable of Attribute instances</p>
<blockquote>
<div><p>Will be stored as a Tuple.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Variable</span><span class="p">,</span> <span class="n">float32</span><span class="p">,</span> <span class="n">integer</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v</span> <span class="o">=</span> <span class="n">Variable</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v</span><span class="o">.</span><span class="n">attrs</span>
<span class="go">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v</span> <span class="o">==</span> <span class="n">Variable</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v</span> <span class="o">==</span> <span class="n">Variable</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="n">float32</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v</span>
<span class="go">Variable(x, type=float32)</span>
</pre></div>
</div>
<p>One may also construct a <code class="docutils literal notranslate"><span class="pre">Variable</span></code> instance with the type deduced from
assumptions about the symbol using the <code class="docutils literal notranslate"><span class="pre">deduced</span></code> classmethod:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">i</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;i&#39;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v</span> <span class="o">=</span> <span class="n">Variable</span><span class="o">.</span><span class="n">deduced</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="n">integer</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v</span> <span class="o">==</span> <span class="n">Variable</span><span class="p">(</span><span class="s1">&#39;i&#39;</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">value_const</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">value_const</span> <span class="ow">in</span> <span class="n">v</span><span class="o">.</span><span class="n">attrs</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">w</span> <span class="o">=</span> <span class="n">Variable</span><span class="p">(</span><span class="s1">&#39;w&#39;</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="p">[</span><span class="n">value_const</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">w</span>
<span class="go">Variable(w, attrs=(value_const,))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">value_const</span> <span class="ow">in</span> <span class="n">w</span><span class="o">.</span><span class="n">attrs</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">w</span><span class="o">.</span><span class="n">as_Declaration</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="mi">42</span><span class="p">)</span>
<span class="go">Declaration(Variable(w, value=42, attrs=(value_const,)))</span>
</pre></div>
</div>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.ast.Variable.as_Declaration">
<span class="sig-name descname"><span class="pre">as_Declaration</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1481-L1511"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Variable.as_Declaration" title="Permalink to this definition">¶</a></dt>
<dd><p>Convenience method for creating a Declaration instance.</p>
<p class="rubric">Explanation</p>
<p>If the variable of the Declaration need to wrap a modified
variable keyword arguments may be passed (overriding e.g.
the <code class="docutils literal notranslate"><span class="pre">value</span></code> of the Variable instance).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Variable</span><span class="p">,</span> <span class="n">NoneToken</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Variable</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">decl1</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">as_Declaration</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># value is special NoneToken() which must be tested with == operator</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">decl1</span><span class="o">.</span><span class="n">variable</span><span class="o">.</span><span class="n">value</span> <span class="ow">is</span> <span class="kc">None</span>  <span class="c1"># won&#39;t work</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">decl1</span><span class="o">.</span><span class="n">variable</span><span class="o">.</span><span class="n">value</span> <span class="o">==</span> <span class="kc">None</span>  <span class="c1"># not PEP-8 compliant</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">decl1</span><span class="o">.</span><span class="n">variable</span><span class="o">.</span><span class="n">value</span> <span class="o">==</span> <span class="n">NoneToken</span><span class="p">()</span>  <span class="c1"># OK</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">decl2</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">as_Declaration</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="mf">42.0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">decl2</span><span class="o">.</span><span class="n">variable</span><span class="o">.</span><span class="n">value</span> <span class="o">==</span> <span class="mi">42</span>
<span class="go">True</span>
</pre></div>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.ast.Variable.deduced">
<em class="property"><span class="pre">classmethod</span> </em><span class="sig-name descname"><span class="pre">deduced</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">symbol</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attrs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cast_check</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1436-L1479"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.Variable.deduced" title="Permalink to this definition">¶</a></dt>
<dd><p>Alt. constructor with type deduction from <code class="docutils literal notranslate"><span class="pre">Type.from_expr</span></code>.</p>
<p>Deduces type primarily from <code class="docutils literal notranslate"><span class="pre">symbol</span></code>, secondarily from <code class="docutils literal notranslate"><span class="pre">value</span></code>.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>symbol</strong> : Symbol</p>
<p><strong>value</strong> : expr</p>
<blockquote>
<div><p>(optional) value of the variable.</p>
</div></blockquote>
<p><strong>attrs</strong> : iterable of Attribute instances</p>
<p><strong>cast_check</strong> : bool</p>
<blockquote>
<div><p>Whether to apply <code class="docutils literal notranslate"><span class="pre">Type.cast_check</span></code> on <code class="docutils literal notranslate"><span class="pre">value</span></code>.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Variable</span><span class="p">,</span> <span class="n">complex_</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;n&#39;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">Variable</span><span class="o">.</span><span class="n">deduced</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">type</span><span class="p">)</span>
<span class="go">&#39;integer&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">real</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v</span> <span class="o">=</span> <span class="n">Variable</span><span class="o">.</span><span class="n">deduced</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v</span><span class="o">.</span><span class="n">type</span>
<span class="go">real</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;z&#39;</span><span class="p">,</span> <span class="nb">complex</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Variable</span><span class="o">.</span><span class="n">deduced</span><span class="p">(</span><span class="n">z</span><span class="p">)</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="n">complex_</span>
<span class="go">True</span>
</pre></div>
</div>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.ast.While">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">While</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L1603-L1638"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.While" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a ‘for-loop’ in the code.</p>
<dl class="simple">
<dt>Expressions are of the form:</dt><dd><dl class="simple">
<dt>“while condition:</dt><dd><p>body…”</p>
</dd>
</dl>
</dd>
</dl>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>condition</strong> : expression convertible to Boolean</p>
<p><strong>body</strong> : CodeBlock or iterable</p>
<blockquote>
<div><p>When passed an iterable it is used to instantiate a CodeBlock.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span><span class="p">,</span> <span class="n">Gt</span><span class="p">,</span> <span class="n">Abs</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen</span> <span class="kn">import</span> <span class="n">aug_assign</span><span class="p">,</span> <span class="n">Assignment</span><span class="p">,</span> <span class="n">While</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">dx</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x dx&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">whl</span> <span class="o">=</span> <span class="n">While</span><span class="p">(</span><span class="n">Gt</span><span class="p">(</span><span class="n">Abs</span><span class="p">(</span><span class="n">dx</span><span class="p">),</span> <span class="mf">1e-9</span><span class="p">),</span> <span class="p">[</span>
<span class="gp">... </span>    <span class="n">Assignment</span><span class="p">(</span><span class="n">dx</span><span class="p">,</span> <span class="o">-</span><span class="n">expr</span><span class="o">/</span><span class="n">expr</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">x</span><span class="p">)),</span>
<span class="gp">... </span>    <span class="n">aug_assign</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="s1">&#39;+&#39;</span><span class="p">,</span> <span class="n">dx</span><span class="p">)</span>
<span class="gp">... </span><span class="p">])</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.codegen.ast.aug_assign">
<span class="sig-prename descclassname"><span class="pre">sympy.codegen.ast.</span></span><span class="sig-name descname"><span class="pre">aug_assign</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">lhs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">op</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">rhs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/ast.py#L540-L580"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.ast.aug_assign" title="Permalink to this definition">¶</a></dt>
<dd><p>Create ‘lhs op= rhs’.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>lhs</strong> : Expr</p>
<blockquote>
<div><p>Sympy object representing the lhs of the expression. These should be
singular objects, such as one would use in writing code. Notable types
include Symbol, MatrixSymbol, MatrixElement, and Indexed. Types that
subclass these types are also supported.</p>
</div></blockquote>
<p><strong>op</strong> : str</p>
<blockquote>
<div><p>Operator (+, -, /, *, %).</p>
</div></blockquote>
<p><strong>rhs</strong> : Expr</p>
<blockquote>
<div><p>Sympy object representing the rhs of the expression. This can be any
type, provided its shape corresponds to that of the lhs. For example,
a Matrix type can be assigned to MatrixSymbol, but not to Symbol, as
the dimensions will not align.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Explanation</p>
<p>Represents augmented variable assignment for code generation. This is a
convenience function. You can also use the AugmentedAssignment classes
directly, like AddAugmentedAssignment(x, y).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">aug_assign</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x, y&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">aug_assign</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="s1">&#39;+&#39;</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="go">AddAugmentedAssignment(x, y)</span>
</pre></div>
</div>
</dd></dl>

</section>
</section>
<section id="module-sympy.codegen.cfunctions">
<span id="special-c-math-functions-sympy-codegen-cfunctions"></span><h2>Special C math functions (sympy.codegen.cfunctions)<a class="headerlink" href="#module-sympy.codegen.cfunctions" title="Permalink to this headline">¶</a></h2>
<p>This module contains SymPy functions mathcin corresponding to special math functions in the
C standard library (since C99, also available in C++11).</p>
<p>The functions defined in this module allows the user to express functions such as <code class="docutils literal notranslate"><span class="pre">expm1</span></code>
as a SymPy function for symbolic manipulation.</p>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cfunctions.Cbrt">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cfunctions.</span></span><span class="sig-name descname"><span class="pre">Cbrt</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cfunctions.py#L439-L483"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cfunctions.Cbrt" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents the cube root function.</p>
<p class="rubric">Explanation</p>
<p>The reason why one would use <code class="docutils literal notranslate"><span class="pre">Cbrt(x)</span></code> over <code class="docutils literal notranslate"><span class="pre">cbrt(x)</span></code>
is that the latter is internally represented as <code class="docutils literal notranslate"><span class="pre">Pow(x,</span> <span class="pre">Rational(1,</span> <span class="pre">3))</span></code> which
may not be what one wants when doing code-generation.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.cfunctions</span> <span class="kn">import</span> <span class="n">Cbrt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Cbrt</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">Cbrt(x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Cbrt</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">1/(3*x**(2/3))</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.codegen.cfunctions.Sqrt" title="sympy.codegen.cfunctions.Sqrt"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Sqrt</span></code></a></p>
</div>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.cfunctions.Cbrt.fdiff">
<span class="sig-name descname"><span class="pre">fdiff</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">argindex</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cfunctions.py#L467-L474"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cfunctions.Cbrt.fdiff" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the first derivative of this function.</p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cfunctions.Sqrt">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cfunctions.</span></span><span class="sig-name descname"><span class="pre">Sqrt</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cfunctions.py#L389-L432"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cfunctions.Sqrt" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents the square root function.</p>
<p class="rubric">Explanation</p>
<p>The reason why one would use <code class="docutils literal notranslate"><span class="pre">Sqrt(x)</span></code> over <code class="docutils literal notranslate"><span class="pre">sqrt(x)</span></code>
is that the latter is internally represented as <code class="docutils literal notranslate"><span class="pre">Pow(x,</span> <span class="pre">S.Half)</span></code> which
may not be what one wants when doing code-generation.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.cfunctions</span> <span class="kn">import</span> <span class="n">Sqrt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">Sqrt(x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">1/(2*sqrt(x))</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.codegen.cfunctions.Cbrt" title="sympy.codegen.cfunctions.Cbrt"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Cbrt</span></code></a></p>
</div>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.cfunctions.Sqrt.fdiff">
<span class="sig-name descname"><span class="pre">fdiff</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">argindex</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cfunctions.py#L417-L424"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cfunctions.Sqrt.fdiff" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the first derivative of this function.</p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cfunctions.exp2">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cfunctions.</span></span><span class="sig-name descname"><span class="pre">exp2</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">arg</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cfunctions.py#L168-L217"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cfunctions.exp2" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents the exponential function with base two.</p>
<p class="rubric">Explanation</p>
<p>The benefit of using <code class="docutils literal notranslate"><span class="pre">exp2(x)</span></code> over <code class="docutils literal notranslate"><span class="pre">2**x</span></code>
is that the latter is not as efficient under finite precision
arithmetic.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.cfunctions</span> <span class="kn">import</span> <span class="n">exp2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">exp2</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span> <span class="o">==</span> <span class="mi">4</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">exp2</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">log(2)*exp2(x)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.codegen.cfunctions.log2" title="sympy.codegen.cfunctions.log2"><code class="xref py py-obj docutils literal notranslate"><span class="pre">log2</span></code></a></p>
</div>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.cfunctions.exp2.fdiff">
<span class="sig-name descname"><span class="pre">fdiff</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">argindex</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cfunctions.py#L197-L204"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cfunctions.exp2.fdiff" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the first derivative of this function.</p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cfunctions.expm1">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cfunctions.</span></span><span class="sig-name descname"><span class="pre">expm1</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">arg</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cfunctions.py#L21-L79"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cfunctions.expm1" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents the exponential function minus one.</p>
<p class="rubric">Explanation</p>
<p>The benefit of using <code class="docutils literal notranslate"><span class="pre">expm1(x)</span></code> over <code class="docutils literal notranslate"><span class="pre">exp(x)</span> <span class="pre">-</span> <span class="pre">1</span></code>
is that the latter is prone to cancellation under finite precision
arithmetic when x is close to zero.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.cfunctions</span> <span class="kn">import</span> <span class="n">expm1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">%.0e</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">expm1</span><span class="p">(</span><span class="mf">1e-99</span><span class="p">)</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span>
<span class="go">&#39;1e-99&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">exp</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">exp</span><span class="p">(</span><span class="mf">1e-99</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
<span class="go">0.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expm1</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">exp(x)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.codegen.cfunctions.log1p" title="sympy.codegen.cfunctions.log1p"><code class="xref py py-obj docutils literal notranslate"><span class="pre">log1p</span></code></a></p>
</div>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.cfunctions.expm1.fdiff">
<span class="sig-name descname"><span class="pre">fdiff</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">argindex</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cfunctions.py#L52-L59"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cfunctions.expm1.fdiff" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the first derivative of this function.</p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cfunctions.fma">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cfunctions.</span></span><span class="sig-name descname"><span class="pre">fma</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cfunctions.py#L288-L326"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cfunctions.fma" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents “fused multiply add”.</p>
<p class="rubric">Explanation</p>
<p>The benefit of using <code class="docutils literal notranslate"><span class="pre">fma(x,</span> <span class="pre">y,</span> <span class="pre">z)</span></code> over <code class="docutils literal notranslate"><span class="pre">x*y</span> <span class="pre">+</span> <span class="pre">z</span></code>
is that, under finite precision arithmetic, the former is
supported by special instructions on some CPUs.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.cfunctions</span> <span class="kn">import</span> <span class="n">fma</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fma</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">y</span>
</pre></div>
</div>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.cfunctions.fma.fdiff">
<span class="sig-name descname"><span class="pre">fdiff</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">argindex</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cfunctions.py#L310-L319"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cfunctions.fma.fdiff" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the first derivative of this function.</p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cfunctions.hypot">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cfunctions.</span></span><span class="sig-name descname"><span class="pre">hypot</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cfunctions.py#L490-L532"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cfunctions.hypot" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents the hypotenuse function.</p>
<p class="rubric">Explanation</p>
<p>The hypotenuse function is provided by e.g. the math library
in the C99 standard, hence one may want to represent the function
symbolically when doing code-generation.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.cfunctions</span> <span class="kn">import</span> <span class="n">hypot</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">hypot</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span> <span class="o">==</span> <span class="mi">5</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">hypot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="go">hypot(x, y)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">hypot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">x/hypot(x, y)</span>
</pre></div>
</div>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.cfunctions.hypot.fdiff">
<span class="sig-name descname"><span class="pre">fdiff</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">argindex</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cfunctions.py#L516-L523"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cfunctions.hypot.fdiff" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the first derivative of this function.</p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cfunctions.log10">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cfunctions.</span></span><span class="sig-name descname"><span class="pre">log10</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">arg</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cfunctions.py#L336-L382"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cfunctions.log10" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents the logarithm function with base ten.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.cfunctions</span> <span class="kn">import</span> <span class="n">log10</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">log10</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span> <span class="o">==</span> <span class="mi">2</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">log10</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">1/(x*log(10))</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.codegen.cfunctions.log2" title="sympy.codegen.cfunctions.log2"><code class="xref py py-obj docutils literal notranslate"><span class="pre">log2</span></code></a></p>
</div>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.cfunctions.log10.fdiff">
<span class="sig-name descname"><span class="pre">fdiff</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">argindex</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cfunctions.py#L357-L364"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cfunctions.log10.fdiff" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the first derivative of this function.</p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cfunctions.log1p">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cfunctions.</span></span><span class="sig-name descname"><span class="pre">log1p</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">arg</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cfunctions.py#L86-L161"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cfunctions.log1p" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents the natural logarithm of a number plus one.</p>
<p class="rubric">Explanation</p>
<p>The benefit of using <code class="docutils literal notranslate"><span class="pre">log1p(x)</span></code> over <code class="docutils literal notranslate"><span class="pre">log(x</span> <span class="pre">+</span> <span class="pre">1)</span></code>
is that the latter is prone to cancellation under finite precision
arithmetic when x is close to zero.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.cfunctions</span> <span class="kn">import</span> <span class="n">log1p</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.core.function</span> <span class="kn">import</span> <span class="n">expand_log</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">%.0e</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">expand_log</span><span class="p">(</span><span class="n">log1p</span><span class="p">(</span><span class="mf">1e-99</span><span class="p">))</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span>
<span class="go">&#39;1e-99&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">log</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">log</span><span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="mf">1e-99</span><span class="p">)</span>
<span class="go">0.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">log1p</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">1/(x + 1)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.codegen.cfunctions.expm1" title="sympy.codegen.cfunctions.expm1"><code class="xref py py-obj docutils literal notranslate"><span class="pre">expm1</span></code></a></p>
</div>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.cfunctions.log1p.fdiff">
<span class="sig-name descname"><span class="pre">fdiff</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">argindex</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cfunctions.py#L119-L126"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cfunctions.log1p.fdiff" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the first derivative of this function.</p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cfunctions.log2">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cfunctions.</span></span><span class="sig-name descname"><span class="pre">log2</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">arg</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cfunctions.py#L224-L281"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cfunctions.log2" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents the logarithm function with base two.</p>
<p class="rubric">Explanation</p>
<p>The benefit of using <code class="docutils literal notranslate"><span class="pre">log2(x)</span></code> over <code class="docutils literal notranslate"><span class="pre">log(x)/log(2)</span></code>
is that the latter is not as efficient under finite precision
arithmetic.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.cfunctions</span> <span class="kn">import</span> <span class="n">log2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">log2</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span> <span class="o">==</span> <span class="mi">2</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">log2</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">1/(x*log(2))</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.codegen.cfunctions.exp2" title="sympy.codegen.cfunctions.exp2"><code class="xref py py-obj docutils literal notranslate"><span class="pre">exp2</span></code></a>, <a class="reference internal" href="#sympy.codegen.cfunctions.log10" title="sympy.codegen.cfunctions.log10"><code class="xref py py-obj docutils literal notranslate"><span class="pre">log10</span></code></a></p>
</div>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.codegen.cfunctions.log2.fdiff">
<span class="sig-name descname"><span class="pre">fdiff</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">argindex</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cfunctions.py#L253-L260"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cfunctions.log2.fdiff" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the first derivative of this function.</p>
</dd></dl>

</dd></dl>

</section>
<section id="module-sympy.codegen.cnodes">
<span id="c-specific-ast-nodes-sympy-codegen-cnodes"></span><h2>C specific AST nodes (sympy.codegen.cnodes)<a class="headerlink" href="#module-sympy.codegen.cnodes" title="Permalink to this headline">¶</a></h2>
<p>AST nodes specific to the C family of languages</p>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cnodes.CommaOperator">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cnodes.</span></span><span class="sig-name descname"><span class="pre">CommaOperator</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cnodes.py#L40-L43"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cnodes.CommaOperator" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents the comma operator in C</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cnodes.Label">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cnodes.</span></span><span class="sig-name descname"><span class="pre">Label</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cnodes.py#L46-L71"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cnodes.Label" title="Permalink to this definition">¶</a></dt>
<dd><p>Label for use with e.g. goto statement.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.cnodes</span> <span class="kn">import</span> <span class="n">Label</span><span class="p">,</span> <span class="n">PreIncrement</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">ccode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ccode</span><span class="p">(</span><span class="n">Label</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">)))</span>
<span class="go">foo:</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ccode</span><span class="p">(</span><span class="n">Label</span><span class="p">(</span><span class="s1">&#39;bar&#39;</span><span class="p">,</span> <span class="p">[</span><span class="n">PreIncrement</span><span class="p">(</span><span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">))])))</span>
<span class="go">bar:</span>
<span class="go">++(a);</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cnodes.PostDecrement">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cnodes.</span></span><span class="sig-name descname"><span class="pre">PostDecrement</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cnodes.py#L96-L98"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cnodes.PostDecrement" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents the post-decrement operator</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cnodes.PostIncrement">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cnodes.</span></span><span class="sig-name descname"><span class="pre">PostIncrement</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cnodes.py#L106-L108"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cnodes.PostIncrement" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents the post-increment operator</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cnodes.PreDecrement">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cnodes.</span></span><span class="sig-name descname"><span class="pre">PreDecrement</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cnodes.py#L80-L93"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cnodes.PreDecrement" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents the pre-decrement operator</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.cnodes</span> <span class="kn">import</span> <span class="n">PreDecrement</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">ccode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ccode</span><span class="p">(</span><span class="n">PreDecrement</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="go">&#39;--(x)&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cnodes.PreIncrement">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cnodes.</span></span><span class="sig-name descname"><span class="pre">PreIncrement</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cnodes.py#L101-L103"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cnodes.PreIncrement" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents the pre-increment operator</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.codegen.cnodes.alignof">
<span class="sig-prename descclassname"><span class="pre">sympy.codegen.cnodes.</span></span><span class="sig-name descname"><span class="pre">alignof</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">arg</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cnodes.py#L20-L22"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cnodes.alignof" title="Permalink to this definition">¶</a></dt>
<dd><p>Generate of FunctionCall instance for calling ‘alignof’</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cnodes.goto">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cnodes.</span></span><span class="sig-name descname"><span class="pre">goto</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cnodes.py#L74-L77"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cnodes.goto" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents goto in C</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.codegen.cnodes.sizeof">
<span class="sig-prename descclassname"><span class="pre">sympy.codegen.cnodes.</span></span><span class="sig-name descname"><span class="pre">sizeof</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">arg</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cnodes.py#L25-L37"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cnodes.sizeof" title="Permalink to this definition">¶</a></dt>
<dd><p>Generate of FunctionCall instance for calling ‘sizeof’</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">real</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.cnodes</span> <span class="kn">import</span> <span class="n">sizeof</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">ccode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ccode</span><span class="p">(</span><span class="n">sizeof</span><span class="p">(</span><span class="n">real</span><span class="p">))</span>
<span class="go">&#39;sizeof(double)&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cnodes.struct">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cnodes.</span></span><span class="sig-name descname"><span class="pre">struct</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cnodes.py#L111-L119"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cnodes.struct" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a struct in C</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cnodes.union">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cnodes.</span></span><span class="sig-name descname"><span class="pre">union</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cnodes.py#L122-L123"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cnodes.union" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a union in C</p>
</dd></dl>

</section>
<section id="module-sympy.codegen.cxxnodes">
<span id="c-specific-ast-nodes-sympy-codegen-cxxnodes"></span><h2>C++ specific AST nodes (sympy.codegen.cxxnodes)<a class="headerlink" href="#module-sympy.codegen.cxxnodes" title="Permalink to this headline">¶</a></h2>
<p>AST nodes specific to C++.</p>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.cxxnodes.using">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.cxxnodes.</span></span><span class="sig-name descname"><span class="pre">using</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cxxnodes.py#L7-L12"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cxxnodes.using" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a ‘using’ statement in C++</p>
</dd></dl>

</section>
<section id="module-sympy.codegen.fnodes">
<span id="fortran-specific-ast-nodes-sympy-codegen-fnodes"></span><h2>Fortran specific AST nodes (sympy.codegen.fnodes)<a class="headerlink" href="#module-sympy.codegen.fnodes" title="Permalink to this headline">¶</a></h2>
<p>AST nodes specific to Fortran.</p>
<p>The functions defined in this module allows the user to express functions such as <code class="docutils literal notranslate"><span class="pre">dsign</span></code>
as a SymPy function for symbolic manipulation.</p>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.ArrayConstructor">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">ArrayConstructor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L213-L229"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.ArrayConstructor" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents an array constructor.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">ArrayConstructor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ac</span> <span class="o">=</span> <span class="n">ArrayConstructor</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fcode</span><span class="p">(</span><span class="n">ac</span><span class="p">,</span> <span class="n">standard</span><span class="o">=</span><span class="mi">95</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">)</span>
<span class="go">&#39;(/1, 2, 3/)&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fcode</span><span class="p">(</span><span class="n">ac</span><span class="p">,</span> <span class="n">standard</span><span class="o">=</span><span class="mi">2003</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">)</span>
<span class="go">&#39;[1, 2, 3]&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.Do">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">Do</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L175-L210"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.Do" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a Do loop in in Fortran.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">aug_assign</span><span class="p">,</span> <span class="n">Print</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">Do</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">i</span><span class="p">,</span> <span class="n">n</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;i n&#39;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;r&#39;</span><span class="p">,</span> <span class="n">real</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">body</span> <span class="o">=</span> <span class="p">[</span><span class="n">aug_assign</span><span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="s1">&#39;+&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="o">/</span><span class="n">i</span><span class="p">),</span> <span class="n">Print</span><span class="p">([</span><span class="n">i</span><span class="p">,</span> <span class="n">r</span><span class="p">])]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">do1</span> <span class="o">=</span> <span class="n">Do</span><span class="p">(</span><span class="n">body</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fcode</span><span class="p">(</span><span class="n">do1</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">))</span>
<span class="go">do i = 1, n</span>
<span class="go">    r = r + 1d0/i</span>
<span class="go">    print *, i, r</span>
<span class="go">end do</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">do2</span> <span class="o">=</span> <span class="n">Do</span><span class="p">(</span><span class="n">body</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fcode</span><span class="p">(</span><span class="n">do2</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">))</span>
<span class="go">do i = 1, n, 2</span>
<span class="go">    r = r + 1d0/i</span>
<span class="go">    print *, i, r</span>
<span class="go">end do</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.Extent">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">Extent</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L256-L287"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.Extent" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a dimension extent.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">Extent</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">e</span> <span class="o">=</span> <span class="n">Extent</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>  <span class="c1"># -3, -2, -1, 0, 1, 2, 3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fcode</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">)</span>
<span class="go">&#39;-3:3&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Variable</span><span class="p">,</span> <span class="n">real</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">dimension</span><span class="p">,</span> <span class="n">intent_out</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dim</span> <span class="o">=</span> <span class="n">dimension</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">arr</span> <span class="o">=</span> <span class="n">Variable</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">real</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="p">[</span><span class="n">dim</span><span class="p">,</span> <span class="n">intent_out</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fcode</span><span class="p">(</span><span class="n">arr</span><span class="o">.</span><span class="n">as_Declaration</span><span class="p">(),</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">,</span> <span class="n">standard</span><span class="o">=</span><span class="mi">2003</span><span class="p">)</span>
<span class="go">&#39;real*8, dimension(-3:3, -3:3), intent(out) :: x&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.FortranReturn">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">FortranReturn</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L553-L576"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.FortranReturn" title="Permalink to this definition">¶</a></dt>
<dd><p>AST node explicitly mapped to a fortran “return”.</p>
<p class="rubric">Explanation</p>
<p>Because a return statement in fortran is different from C, and
in order to aid reuse of our codegen ASTs the ordinary
<code class="docutils literal notranslate"><span class="pre">.codegen.ast.Return</span></code> is interpreted as assignment to
the result variable of the function. If one for some reason needs
to generate a fortran RETURN statement, this node should be used.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">FortranReturn</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fcode</span><span class="p">(</span><span class="n">FortranReturn</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">))</span>
<span class="go">&#39;       return x&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.GoTo">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">GoTo</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L534-L550"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.GoTo" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a goto statement in Fortran</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">GoTo</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">go</span> <span class="o">=</span> <span class="n">GoTo</span><span class="p">([</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">30</span><span class="p">],</span> <span class="s1">&#39;i&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fcode</span><span class="p">(</span><span class="n">go</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">)</span>
<span class="go">&#39;go to (10, 20, 30), i&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.ImpliedDoLoop">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">ImpliedDoLoop</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L232-L253"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.ImpliedDoLoop" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents an implied do loop in Fortran.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span><span class="p">,</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">ImpliedDoLoop</span><span class="p">,</span> <span class="n">ArrayConstructor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">i</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;i&#39;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">idl</span> <span class="o">=</span> <span class="n">ImpliedDoLoop</span><span class="p">(</span><span class="n">i</span><span class="o">**</span><span class="mi">3</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>  <span class="c1"># -27, -1, 1, 27</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ac</span> <span class="o">=</span> <span class="n">ArrayConstructor</span><span class="p">([</span><span class="o">-</span><span class="mi">28</span><span class="p">,</span> <span class="n">idl</span><span class="p">,</span> <span class="mi">28</span><span class="p">])</span> <span class="c1"># -28, -27, -1, 1, 27, 28</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fcode</span><span class="p">(</span><span class="n">ac</span><span class="p">,</span> <span class="n">standard</span><span class="o">=</span><span class="mi">2003</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">)</span>
<span class="go">&#39;[-28, (i**3, i = -3, 3, 2), 28]&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.Module">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">Module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L102-L124"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.Module" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a module in Fortran.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">Module</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fcode</span><span class="p">(</span><span class="n">Module</span><span class="p">(</span><span class="s1">&#39;signallib&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;implicit none&#39;</span><span class="p">],</span> <span class="p">[]),</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">))</span>
<span class="go">module signallib</span>
<span class="go">implicit none</span>

<span class="go">contains</span>


<span class="go">end module</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.Program">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">Program</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L32-L50"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.Program" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a ‘program’ block in Fortran.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Print</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">Program</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">prog</span> <span class="o">=</span> <span class="n">Program</span><span class="p">(</span><span class="s1">&#39;myprogram&#39;</span><span class="p">,</span> <span class="p">[</span><span class="n">Print</span><span class="p">([</span><span class="mi">42</span><span class="p">])])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fcode</span><span class="p">(</span><span class="n">prog</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">))</span>
<span class="go">program myprogram</span>
<span class="go">    print *, 42</span>
<span class="go">end program</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.Subroutine">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">Subroutine</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L127-L156"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.Subroutine" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a subroutine in Fortran.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Print</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">Subroutine</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y&#39;</span><span class="p">,</span> <span class="n">real</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sub</span> <span class="o">=</span> <span class="n">Subroutine</span><span class="p">(</span><span class="s1">&#39;mysub&#39;</span><span class="p">,</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">],</span> <span class="p">[</span><span class="n">Print</span><span class="p">([</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">])])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fcode</span><span class="p">(</span><span class="n">sub</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">,</span> <span class="n">standard</span><span class="o">=</span><span class="mi">2003</span><span class="p">))</span>
<span class="go">subroutine mysub(x, y)</span>
<span class="go">real*8 :: x</span>
<span class="go">real*8 :: y</span>
<span class="go">print *, x**2 + y**2, x*y</span>
<span class="go">end subroutine</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.SubroutineCall">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">SubroutineCall</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L158-L172"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.SubroutineCall" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a call to a subroutine in Fortran.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">SubroutineCall</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fcode</span><span class="p">(</span><span class="n">SubroutineCall</span><span class="p">(</span><span class="s1">&#39;mysub&#39;</span><span class="p">,</span> <span class="s1">&#39;x y&#39;</span><span class="o">.</span><span class="n">split</span><span class="p">()))</span>
<span class="go">&#39;       call mysub(x, y)&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.allocated">
<span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">allocated</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">array</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L378-L391"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.allocated" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates an AST node for a function call to Fortran’s “allocated(…)”</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">allocated</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">alloc</span> <span class="o">=</span> <span class="n">allocated</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fcode</span><span class="p">(</span><span class="n">alloc</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">)</span>
<span class="go">&#39;allocated(x)&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.array">
<span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">array</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">symbol</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dim</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">intent</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attrs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">type</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L329-L372"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.array" title="Permalink to this definition">¶</a></dt>
<dd><p>Convenience function for creating a Variable instance for a Fortran array.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>symbol</strong> : symbol</p>
<p><strong>dim</strong> : Attribute or iterable</p>
<blockquote>
<div><p>If dim is an <code class="docutils literal notranslate"><span class="pre">Attribute</span></code> it need to have the name ‘dimension’. If it is
not an <code class="docutils literal notranslate"><span class="pre">Attribute</span></code>, then it is passsed to <a class="reference internal" href="#sympy.codegen.fnodes.dimension" title="sympy.codegen.fnodes.dimension"><code class="xref py py-func docutils literal notranslate"><span class="pre">dimension()</span></code></a> as <code class="docutils literal notranslate"><span class="pre">*dim</span></code></p>
</div></blockquote>
<p><strong>intent</strong> : str</p>
<blockquote>
<div><p>One of: ‘in’, ‘out’, ‘inout’ or None</p>
</div></blockquote>
<p><strong>**kwargs:</strong></p>
<blockquote>
<div><p>Keyword arguments for <code class="docutils literal notranslate"><span class="pre">Variable</span></code> (‘type’ &amp; ‘value’)</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">integer</span><span class="p">,</span> <span class="n">real</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">array</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">arr</span> <span class="o">=</span> <span class="n">array</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;*&#39;</span><span class="p">,</span> <span class="s1">&#39;in&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="n">integer</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fcode</span><span class="p">(</span><span class="n">arr</span><span class="o">.</span><span class="n">as_Declaration</span><span class="p">(),</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">,</span> <span class="n">standard</span><span class="o">=</span><span class="mi">2003</span><span class="p">))</span>
<span class="go">integer*4, dimension(*), intent(in) :: a</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">array</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="s1">&#39;:&#39;</span><span class="p">,</span> <span class="s1">&#39;:&#39;</span><span class="p">],</span> <span class="n">intent</span><span class="o">=</span><span class="s1">&#39;out&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="n">real</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fcode</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">as_Declaration</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="mi">1</span><span class="p">),</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">,</span> <span class="n">standard</span><span class="o">=</span><span class="mi">2003</span><span class="p">))</span>
<span class="go">real*8, dimension(3, :, :), intent(out) :: x = 1</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.bind_C">
<span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">bind_C</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L504-L532"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.bind_C" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates an Attribute <code class="docutils literal notranslate"><span class="pre">bind_C</span></code> with a name.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>name</strong> : str</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">FunctionDefinition</span><span class="p">,</span> <span class="n">real</span><span class="p">,</span> <span class="n">Return</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">array</span><span class="p">,</span> <span class="n">sum_</span><span class="p">,</span> <span class="n">bind_C</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="n">real</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;s&#39;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">arr</span> <span class="o">=</span> <span class="n">array</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">dim</span><span class="o">=</span><span class="p">[</span><span class="n">s</span><span class="p">],</span> <span class="n">intent</span><span class="o">=</span><span class="s1">&#39;in&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">body</span> <span class="o">=</span> <span class="p">[</span><span class="n">Return</span><span class="p">((</span><span class="n">sum_</span><span class="p">(</span><span class="n">a</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">/</span><span class="n">s</span><span class="p">)</span><span class="o">**</span><span class="mf">.5</span><span class="p">)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fd</span> <span class="o">=</span> <span class="n">FunctionDefinition</span><span class="p">(</span><span class="n">real</span><span class="p">,</span> <span class="s1">&#39;rms&#39;</span><span class="p">,</span> <span class="p">[</span><span class="n">arr</span><span class="p">,</span> <span class="n">s</span><span class="p">],</span> <span class="n">body</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="p">[</span><span class="n">bind_C</span><span class="p">(</span><span class="s1">&#39;rms&#39;</span><span class="p">)])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fcode</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">,</span> <span class="n">standard</span><span class="o">=</span><span class="mi">2003</span><span class="p">))</span>
<span class="go">real*8 function rms(a, s) bind(C, name=&quot;rms&quot;)</span>
<span class="go">real*8, dimension(s), intent(in) :: a</span>
<span class="go">integer*4 :: s</span>
<span class="go">rms = sqrt(sum(a**2)/s)</span>
<span class="go">end function</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.cmplx">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">cmplx</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L604-L606"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.cmplx" title="Permalink to this definition">¶</a></dt>
<dd><p>Fortran complex conversion function.</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.dimension">
<span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">dimension</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L292-L324"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.dimension" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates a ‘dimension’ Attribute with (up to 7) extents.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">dimension</span><span class="p">,</span> <span class="n">intent_in</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dim</span> <span class="o">=</span> <span class="n">dimension</span><span class="p">(</span><span class="s1">&#39;2&#39;</span><span class="p">,</span> <span class="s1">&#39;:&#39;</span><span class="p">)</span>  <span class="c1"># 2 rows, runtime determined number of columns</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Variable</span><span class="p">,</span> <span class="n">integer</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">arr</span> <span class="o">=</span> <span class="n">Variable</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="n">integer</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="p">[</span><span class="n">dim</span><span class="p">,</span> <span class="n">intent_in</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fcode</span><span class="p">(</span><span class="n">arr</span><span class="o">.</span><span class="n">as_Declaration</span><span class="p">(),</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">,</span> <span class="n">standard</span><span class="o">=</span><span class="mi">2003</span><span class="p">)</span>
<span class="go">&#39;integer*4, dimension(2, :), intent(in) :: a&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.dsign">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">dsign</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L599-L601"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.dsign" title="Permalink to this definition">¶</a></dt>
<dd><p>Fortran sign intrinsic for double precision arguments.</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.isign">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">isign</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L594-L596"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.isign" title="Permalink to this definition">¶</a></dt>
<dd><p>Fortran sign intrinsic for integer arguments.</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.kind">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">kind</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L609-L611"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.kind" title="Permalink to this definition">¶</a></dt>
<dd><p>Fortran kind function.</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.lbound">
<span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">lbound</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">array</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dim</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kind</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L394-L419"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.lbound" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates an AST node for a function call to Fortran’s “lbound(…)”</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>array</strong> : Symbol or String</p>
<p><strong>dim</strong> : expr</p>
<p><strong>kind</strong> : expr</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">lbound</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">lb</span> <span class="o">=</span> <span class="n">lbound</span><span class="p">(</span><span class="s1">&#39;arr&#39;</span><span class="p">,</span> <span class="n">dim</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fcode</span><span class="p">(</span><span class="n">lb</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">)</span>
<span class="go">&#39;lbound(arr, 2)&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.literal_dp">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">literal_dp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">num</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dps</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prec</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">precision</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L637-L640"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.literal_dp" title="Permalink to this definition">¶</a></dt>
<dd><p>Fortran double precision real literal</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.literal_sp">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">literal_sp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">num</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dps</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prec</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">precision</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L631-L634"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.literal_sp" title="Permalink to this definition">¶</a></dt>
<dd><p>Fortran single precision real literal</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.merge">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">merge</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L614-L616"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.merge" title="Permalink to this definition">¶</a></dt>
<dd><p>Fortran merge function</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.reshape">
<span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">reshape</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">shape</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pad</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">order</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L486-L501"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.reshape" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates an AST node for a function call to Fortran’s “reshape(…)”</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>source</strong> : Symbol or String</p>
<p><strong>shape</strong> : ArrayExpr</p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.shape">
<span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">shape</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kind</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L431-L454"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.shape" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates an AST node for a function call to Fortran’s “shape(…)”</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>source</strong> : Symbol or String</p>
<p><strong>kind</strong> : expr</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">shape</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">shp</span> <span class="o">=</span> <span class="n">shape</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fcode</span><span class="p">(</span><span class="n">shp</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">)</span>
<span class="go">&#39;shape(x)&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.size">
<span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">size</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">array</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dim</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kind</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L457-L483"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.size" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates an AST node for a function call to Fortran’s “size(…)”</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">FunctionDefinition</span><span class="p">,</span> <span class="n">real</span><span class="p">,</span> <span class="n">Return</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">array</span><span class="p">,</span> <span class="n">sum_</span><span class="p">,</span> <span class="n">size</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="n">real</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">body</span> <span class="o">=</span> <span class="p">[</span><span class="n">Return</span><span class="p">((</span><span class="n">sum_</span><span class="p">(</span><span class="n">a</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">/</span><span class="n">size</span><span class="p">(</span><span class="n">a</span><span class="p">))</span><span class="o">**</span><span class="mf">.5</span><span class="p">)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">arr</span> <span class="o">=</span> <span class="n">array</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">dim</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;:&#39;</span><span class="p">],</span> <span class="n">intent</span><span class="o">=</span><span class="s1">&#39;in&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fd</span> <span class="o">=</span> <span class="n">FunctionDefinition</span><span class="p">(</span><span class="n">real</span><span class="p">,</span> <span class="s1">&#39;rms&#39;</span><span class="p">,</span> <span class="p">[</span><span class="n">arr</span><span class="p">],</span> <span class="n">body</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fcode</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">,</span> <span class="n">standard</span><span class="o">=</span><span class="mi">2003</span><span class="p">))</span>
<span class="go">real*8 function rms(a)</span>
<span class="go">real*8, dimension(:), intent(in) :: a</span>
<span class="go">rms = sqrt(sum(a**2)*1d0/size(a))</span>
<span class="go">end function</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.use">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">use</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L79-L99"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.use" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a use statement in Fortran.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">use</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fcode</span><span class="p">(</span><span class="n">use</span><span class="p">(</span><span class="s1">&#39;signallib&#39;</span><span class="p">),</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">)</span>
<span class="go">&#39;use signallib&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fcode</span><span class="p">(</span><span class="n">use</span><span class="p">(</span><span class="s1">&#39;signallib&#39;</span><span class="p">,</span> <span class="p">[(</span><span class="s1">&#39;metric&#39;</span><span class="p">,</span> <span class="s1">&#39;snr&#39;</span><span class="p">)]),</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">)</span>
<span class="go">&#39;use signallib, metric =&gt; snr&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fcode</span><span class="p">(</span><span class="n">use</span><span class="p">(</span><span class="s1">&#39;signallib&#39;</span><span class="p">,</span> <span class="n">only</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;snr&#39;</span><span class="p">,</span> <span class="s1">&#39;convolution2d&#39;</span><span class="p">]),</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">)</span>
<span class="go">&#39;use signallib, only: snr, convolution2d&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.codegen.fnodes.use_rename">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.codegen.fnodes.</span></span><span class="sig-name descname"><span class="pre">use_rename</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/fnodes.py#L53-L71"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.fnodes.use_rename" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a renaming in a use statement in Fortran.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.fnodes</span> <span class="kn">import</span> <span class="n">use_rename</span><span class="p">,</span> <span class="n">use</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing</span> <span class="kn">import</span> <span class="n">fcode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ren</span> <span class="o">=</span> <span class="n">use_rename</span><span class="p">(</span><span class="s2">&quot;thingy&quot;</span><span class="p">,</span> <span class="s2">&quot;convolution2d&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fcode</span><span class="p">(</span><span class="n">ren</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">))</span>
<span class="go">thingy =&gt; convolution2d</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">full</span> <span class="o">=</span> <span class="n">use</span><span class="p">(</span><span class="s1">&#39;signallib&#39;</span><span class="p">,</span> <span class="n">only</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;snr&#39;</span><span class="p">,</span> <span class="n">ren</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fcode</span><span class="p">(</span><span class="n">full</span><span class="p">,</span> <span class="n">source_format</span><span class="o">=</span><span class="s1">&#39;free&#39;</span><span class="p">))</span>
<span class="go">use signallib, only: snr, thingy =&gt; convolution2d</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="module-sympy.codegen.algorithms">
<span id="algorithms-sympy-codegen-algorithms"></span><h2>Algorithms (sympy.codegen.algorithms)<a class="headerlink" href="#module-sympy.codegen.algorithms" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.codegen.algorithms.newtons_method">
<span class="sig-prename descclassname"><span class="pre">sympy.codegen.algorithms.</span></span><span class="sig-name descname"><span class="pre">newtons_method</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">wrt</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">atol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1e-12</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">delta</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">debug</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">itermax</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">counter</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/algorithms.py#L9-L78"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.algorithms.newtons_method" title="Permalink to this definition">¶</a></dt>
<dd><p>Generates an AST for Newton-Raphson method (a root-finding algorithm).</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>expr</strong> : expression</p>
<p><strong>wrt</strong> : Symbol</p>
<blockquote>
<div><p>With respect to, i.e. what is the variable.</p>
</div></blockquote>
<p><strong>atol</strong> : number or expr</p>
<blockquote>
<div><p>Absolute tolerance (stopping criterion)</p>
</div></blockquote>
<p><strong>delta</strong> : Symbol</p>
<blockquote>
<div><p>Will be a <code class="docutils literal notranslate"><span class="pre">Dummy</span></code> if <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
</div></blockquote>
<p><strong>debug</strong> : bool</p>
<blockquote>
<div><p>Whether to print convergence information during iterations</p>
</div></blockquote>
<p><strong>itermax</strong> : number or expr</p>
<blockquote>
<div><p>Maximum number of iterations.</p>
</div></blockquote>
<p><strong>counter</strong> : Symbol</p>
<blockquote>
<div><p>Will be a <code class="docutils literal notranslate"><span class="pre">Dummy</span></code> if <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Explanation</p>
<p>Returns an abstract syntax tree (AST) based on <code class="docutils literal notranslate"><span class="pre">sympy.codegen.ast</span></code> for Netwon’s
method of root-finding.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span><span class="p">,</span> <span class="n">cos</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.ast</span> <span class="kn">import</span> <span class="n">Assignment</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.algorithms</span> <span class="kn">import</span> <span class="n">newtons_method</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">dx</span><span class="p">,</span> <span class="n">atol</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x dx atol&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">-</span> <span class="n">x</span><span class="o">**</span><span class="mi">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">algo</span> <span class="o">=</span> <span class="n">newtons_method</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">atol</span><span class="p">,</span> <span class="n">dx</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">algo</span><span class="o">.</span><span class="n">has</span><span class="p">(</span><span class="n">Assignment</span><span class="p">(</span><span class="n">dx</span><span class="p">,</span> <span class="o">-</span><span class="n">expr</span><span class="o">/</span><span class="n">expr</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">x</span><span class="p">)))</span>
<span class="go">True</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r37"><span class="brackets"><a class="fn-backref" href="#id3">R37</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Newton%27s_method">https://en.wikipedia.org/wiki/Newton%27s_method</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.codegen.algorithms.newtons_method_function">
<span class="sig-prename descclassname"><span class="pre">sympy.codegen.algorithms.</span></span><span class="sig-name descname"><span class="pre">newtons_method_function</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">wrt</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">params</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">func_name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'newton'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attrs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">delta</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/algorithms.py#L89-L146"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.algorithms.newtons_method_function" title="Permalink to this definition">¶</a></dt>
<dd><p>Generates an AST for a function implementing the Newton-Raphson method.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>expr</strong> : expression</p>
<p><strong>wrt</strong> : Symbol</p>
<blockquote>
<div><p>With respect to, i.e. what is the variable</p>
</div></blockquote>
<p><strong>params</strong> : iterable of symbols</p>
<blockquote>
<div><p>Symbols appearing in expr that are taken as constants during the iterations
(these will be accepted as parameters to the generated function).</p>
</div></blockquote>
<p><strong>func_name</strong> : str</p>
<blockquote>
<div><p>Name of the generated function.</p>
</div></blockquote>
<p><strong>attrs</strong> : Tuple</p>
<blockquote>
<div><p>Attribute instances passed as <code class="docutils literal notranslate"><span class="pre">attrs</span></code> to <code class="docutils literal notranslate"><span class="pre">FunctionDefinition</span></code>.</p>
</div></blockquote>
<p><strong>**kwargs :</strong></p>
<blockquote>
<div><p>Keyword arguments passed to <a class="reference internal" href="#sympy.codegen.algorithms.newtons_method" title="sympy.codegen.algorithms.newtons_method"><code class="xref py py-func docutils literal notranslate"><span class="pre">sympy.codegen.algorithms.newtons_method()</span></code></a>.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span><span class="p">,</span> <span class="n">cos</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.algorithms</span> <span class="kn">import</span> <span class="n">newtons_method_function</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.codegen.pyutils</span> <span class="kn">import</span> <span class="n">render_as_module</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">-</span> <span class="n">x</span><span class="o">**</span><span class="mi">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">func</span> <span class="o">=</span> <span class="n">newtons_method_function</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">py_mod</span> <span class="o">=</span> <span class="n">render_as_module</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>  <span class="c1"># source code as string</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">namespace</span> <span class="o">=</span> <span class="p">{}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">exec</span><span class="p">(</span><span class="n">py_mod</span><span class="p">,</span> <span class="n">namespace</span><span class="p">,</span> <span class="n">namespace</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">res</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="s1">&#39;newton(0.5)&#39;</span><span class="p">,</span> <span class="n">namespace</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">abs</span><span class="p">(</span><span class="n">res</span> <span class="o">-</span> <span class="mf">0.865474033102</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1e-12</span>
<span class="go">True</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.codegen.algorithms.newtons_method" title="sympy.codegen.algorithms.newtons_method"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.codegen.algorithms.newtons_method</span></code></a></p>
</div>
</dd></dl>

</section>
<section id="module-sympy.codegen.pyutils">
<span id="python-utilities-sympy-codegen-pyutils"></span><h2>Python utilities (sympy.codegen.pyutils)<a class="headerlink" href="#module-sympy.codegen.pyutils" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.codegen.pyutils.render_as_module">
<span class="sig-prename descclassname"><span class="pre">sympy.codegen.pyutils.</span></span><span class="sig-name descname"><span class="pre">render_as_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">content</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">standard</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'python3'</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/pyutils.py#L6-L24"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.pyutils.render_as_module" title="Permalink to this definition">¶</a></dt>
<dd><p>Renders python code as a module (with the required imports).</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>standard :</strong></p>
<blockquote>
<div><p>See the parameter <code class="docutils literal notranslate"><span class="pre">standard</span></code> in
<a class="reference internal" href="printing.html#sympy.printing.pycode.pycode" title="sympy.printing.pycode.pycode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sympy.printing.pycode.pycode()</span></code></a></p>
</div></blockquote>
</dd>
</dl>
</dd></dl>

</section>
<section id="module-sympy.codegen.cutils">
<span id="c-utilities-sympy-codegen-cutils"></span><h2>C utilities (sympy.codegen.cutils)<a class="headerlink" href="#module-sympy.codegen.cutils" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.codegen.cutils.render_as_source_file">
<span class="sig-prename descclassname"><span class="pre">sympy.codegen.cutils.</span></span><span class="sig-name descname"><span class="pre">render_as_source_file</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">content</span></em>, <em class="sig-param"><span class="pre">Printer=&lt;class</span> <span class="pre">'sympy.printing.c.C99CodePrinter'&gt;</span></em>, <em class="sig-param"><span class="pre">settings=None</span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/cutils.py#L3-L8"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.cutils.render_as_source_file" title="Permalink to this definition">¶</a></dt>
<dd><p>Renders a C source file (with required #include statements)</p>
</dd></dl>

</section>
<section id="module-sympy.codegen.futils">
<span id="fortran-utilities-sympy-codegen-futils"></span><h2>Fortran utilities (sympy.codegen.futils)<a class="headerlink" href="#module-sympy.codegen.futils" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.codegen.futils.render_as_module">
<span class="sig-prename descclassname"><span class="pre">sympy.codegen.futils.</span></span><span class="sig-name descname"><span class="pre">render_as_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">definitions</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">declarations</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">printer_settings</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/codegen/futils.py#L9-L40"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.codegen.futils.render_as_module" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates a <code class="docutils literal notranslate"><span class="pre">Module</span></code> instance and renders it as a string.</p>
<p>This generates Fortran source code for a module with the correct <code class="docutils literal notranslate"><span class="pre">use</span></code> statements.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>definitions</strong> : iterable</p>
<blockquote>
<div><p>Passed to <a class="reference internal" href="#sympy.codegen.fnodes.Module" title="sympy.codegen.fnodes.Module"><code class="xref py py-class docutils literal notranslate"><span class="pre">sympy.codegen.fnodes.Module</span></code></a>.</p>
</div></blockquote>
<p><strong>name</strong> : str</p>
<blockquote>
<div><p>Passed to <a class="reference internal" href="#sympy.codegen.fnodes.Module" title="sympy.codegen.fnodes.Module"><code class="xref py py-class docutils literal notranslate"><span class="pre">sympy.codegen.fnodes.Module</span></code></a>.</p>
</div></blockquote>
<p><strong>declarations</strong> : iterable</p>
<blockquote>
<div><p>Passed to <a class="reference internal" href="#sympy.codegen.fnodes.Module" title="sympy.codegen.fnodes.Module"><code class="xref py py-class docutils literal notranslate"><span class="pre">sympy.codegen.fnodes.Module</span></code></a>. It will be extended with
use statements, ‘implicit none’ and public list generated from <code class="docutils literal notranslate"><span class="pre">definitions</span></code>.</p>
</div></blockquote>
<p><strong>printer_settings</strong> : dict</p>
<blockquote>
<div><p>Passed to <code class="docutils literal notranslate"><span class="pre">FCodePrinter</span></code> (default: <code class="docutils literal notranslate"><span class="pre">{'standard':</span> <span class="pre">2003,</span> <span class="pre">'source_format':</span> <span class="pre">'free'}</span></code>).</p>
</div></blockquote>
</dd>
</dl>
</dd></dl>

</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Code Generation</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#code-printers-sympy-printing">Code printers (sympy.printing)</a></li>
<li><a class="reference internal" href="#codegen-sympy-utilities-codegen">Codegen (sympy.utilities.codegen)</a></li>
<li><a class="reference internal" href="#autowrap">Autowrap</a></li>
<li><a class="reference internal" href="#module-sympy.codegen.rewriting">Classes and functions for rewriting expressions (sympy.codegen.rewriting)</a></li>
<li><a class="reference internal" href="#module-sympy.codegen.approximations">Tools for simplifying expressions using approximations (sympy.codegen.approximations)</a></li>
<li><a class="reference internal" href="#module-sympy.codegen.ast">Classes for abstract syntax trees (sympy.codegen.ast)</a><ul>
<li><a class="reference internal" href="#ast-type-tree">AST Type Tree</a></li>
<li><a class="reference internal" href="#predefined-types">Predefined types</a></li>
<li><a class="reference internal" href="#using-the-nodes">Using the nodes</a></li>
</ul>
</li>
<li><a class="reference internal" href="#module-sympy.codegen.cfunctions">Special C math functions (sympy.codegen.cfunctions)</a></li>
<li><a class="reference internal" href="#module-sympy.codegen.cnodes">C specific AST nodes (sympy.codegen.cnodes)</a></li>
<li><a class="reference internal" href="#module-sympy.codegen.cxxnodes">C++ specific AST nodes (sympy.codegen.cxxnodes)</a></li>
<li><a class="reference internal" href="#module-sympy.codegen.fnodes">Fortran specific AST nodes (sympy.codegen.fnodes)</a></li>
<li><a class="reference internal" href="#module-sympy.codegen.algorithms">Algorithms (sympy.codegen.algorithms)</a></li>
<li><a class="reference internal" href="#module-sympy.codegen.pyutils">Python utilities (sympy.codegen.pyutils)</a></li>
<li><a class="reference internal" href="#module-sympy.codegen.cutils">C utilities (sympy.codegen.cutils)</a></li>
<li><a class="reference internal" href="#module-sympy.codegen.futils">Fortran utilities (sympy.codegen.futils)</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="categories.html"
                        title="previous chapter">Category Theory</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="combinatorics/index.html"
                        title="next chapter">Combinatorics</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/modules/codegen.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="combinatorics/index.html" title="Combinatorics"
             >next</a> |</li>
        <li class="right" >
          <a href="categories.html" title="Category Theory"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" >SymPy Modules Reference</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Code Generation</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/modules/codegen.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:24:07 GMT -->
</html>